mardi 28 novembre 2023

Facing a problem in a coding and decoding words in secret language kinda project [closed]

The problem here is in decoding. When I encode the word "hello" it gives me this output "ollehhello". What's wrong? I know it will not work for sentences. I tried to decode everything i encode but I think I'm making a mistake somewhere. I included random symbols numbers and letters in starting and at end of the word and inserted a symbol after each char of the word.

import random
import string

def get_random_symbols(length):
    return ''.join(random.choice(["<", ">", "?", "/", ";", ".", "*", "^", "&", "(", ")", "_", "!", "@", "#", "4", "%", "^", "&"]) for _ in range(length))

def get_random_numbers(length):
    return ''.join(random.choice(["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"]) for _ in range(length))

def get_random_letters(length):
    return ''.join(random.choice(string.ascii_lowercase) for _ in range(length))

def reverse(x):
    return x[::-1]

def transform_word(word):
    transformed_word = ''
    for letter in word:
        transformed_word += letter + get_random_symbols(1)
    return transformed_word


def transformation(text):
    output = []
    for word in text.split():
        if len(word) <= 3:
            word = (
                get_random_numbers(1) +
                reverse(word) +
                get_random_symbols(1) +
                get_random_letters(1)
            )
            output.append(word)
        else:
            transformed_word = (
                # get_random_numbers(2) +
                get_random_letters(2) +
                get_random_symbols(1) +
                reverse(transform_word(word)) +
                get_random_symbols(1) +
                get_random_letters(1)
            )
            output.append(transformed_word)
    return ' '.join(output)

def decoded_transformation(encoded_code):

    def remove_unnecessary_elements(word):
      word_after_removal = word[3:-2]
      return word_after_removal

  
    def remove_symbols(word):
        return word.replace('<', '').replace('>', '').replace('?', '').replace('/', '').replace(';', '').replace('.', '').replace('*', '').replace('^', '').replace('&', '').replace('(', '').replace(')', '').replace('_', '')


    def remove_numbers(word):
        return word.replace('1', '').replace('2', '').replace('3', '').replace('4', '').replace('5', '').replace('6', '').replace('7', '').replace('8', '').replace('9', '').replace('0', '')
      

    decoded_output = []
    for word in encoded_code.split():
        if len(word) <= 5:
            decoded_word = reverse(remove_symbols(remove_numbers(word)))
        else:
            decoded_word = remove_unnecessary_elements(word)
            decoded_word = remove_symbols(remove_numbers(decoded_word))
            decoded_word += reverse(decoded_word)
        decoded_output.append(decoded_word)
    return ' '.join(decoded_output)

def code(user_input):
    return transformation(user_input)

def decode(user_input):
    return decoded_transformation(user_input)

command = input("What do you want to do, code or decode? Type 'code' or 'decode' : ")

if command.lower() == "code":
    user_input = input("Enter the message to be coded: ")
    print("The coded message is :\n", code(user_input))
elif command.lower() == "decode":
    user_input = input("Enter the message to be decoded: ")
    print("The original message was :\n", decode(user_input))
else:
    print("Wrong command given")

input("Press enter to exit")

I tried to research it on chatgpt and google but I am unable to find a way to get "hello".




Aucun commentaire:

Enregistrer un commentaire