Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

make a python program that has a list of charracters, then it dynamically, based on a…

  1. User

    make a python program that has a list of charracters, then it dynamically, based on a seed created a char map from all the base64 possible chars and the list of chars and dynamically generated a char mapping. then it takes in an input string and it either outputs it encoded with the char map or decoded using the char map

  2. Assistant

    Certainly! Here's a Python program that generates a character map based on a given seed and allows you to encode or decode a given string using that character map:

    import random
    import base64
    
    # List of characters for the mapping
    characters = ['!', '@', '#', '$', '%', '&', '*', '+', '-', '/', '=', '?']
    
    def generate_char_map(seed):
        # Set seed for random generator
        random.seed(seed)
        
        # Generate a shuffled list of base64 characters
        base64_chars = [c for c in base64.b64encode(bytes(range(256))).decode('utf-8')]
        random.shuffle(base64_chars)
        
        # Create the character mapping
        char_map = {}
        for i, char in enumerate(characters):
            char_map[char] = base64_chars[i]
        return char_map
    
    def encode_string(char_map, string):
        encoded_string = ""
        for char in string:
            if char in char_map:
                encoded_string += char_map[char]
            else:
                encoded_string += char
        return encoded_string
    
    def decode_string(char_map, string):
        decoded_string = ""
        for char in string:
            if char in char_map.values():
                for key, value in char_map.items():
                    if value == char:
                        decoded_string += key
                        break
            else:
                decoded_string += char
        return decoded_string
    
    # Seed to generate the character map
    seed = 42
    char_map = generate_char_map(seed)
    
    # Example input string
    input_string = "Hello, World!"
    
    # Encode the input string
    encoded_string = encode_string(char_map, input_string)
    print("Encoded String:", encoded_string)
    
    # Decode the encoded string
    decoded_string = decode_string(char_map, encoded_string)
    print("Decoded String:", decoded_string)
    

    In this program, the characters list contains the characters you want to use for the mapping. The generate_char_map(seed) function generates a character map by randomly shuffling the base64 characters and mapping them to the characters from the characters list using the given seed. The encode_string(char_map, string) function encodes the input string using the generated character map, while the decode_string(char_map, string) function decodes the encoded string using the same character map.

    Let me know if you need any further clarification.