TransWikia.com

Why are the spacings in the decipher incorrect?

Stack Overflow Asked by iLikeToCode123465 on January 4, 2021

I have created a decipher.

code = input("What text would you like to decipher: ")
number_of_gos = 0
code_listed = []
string = ""

def change_string():
    global string
    code_listed.append(string)
    string = ""
    return string

for n in code:
    if n == " ":
        pass
    else:
        if number_of_gos % 4 == 0 and number_of_gos > 0:
            string = change_string()
        string += n
        number_of_gos += 1

code_listed.append(string)

chars = {
    "uytv": "a",
    "ghas": "b",
    "opts": "c",
    "qwra": "d",
    "bcvb": "e",
    "wdrs": "f",
    "rwes": "g",
    "teya": "h",
    "uisd": "i",
    "hgnl": "j",
    "mvbn": "k",
    "onhm": "l",
    "ponu": "o",
    "gbho": "m",
    "wesg": "n",
    "idgf": "p",
    "asfb": "q",
    "nbnf": "r",
    "dngd": "s",
    "nbvd": "t",
    "mbnf": "u",
    "ignf": "v",
    "fddf": "w",
    "sabd": "x",
    "asda": "y",
    "qfjy": "z"
}

new_code = ""
for char in code_listed:
    new_char = chars[char]
    new_code += new_char

for char in code:
    if char == " ":
        code = list(code)
        char_index = code.index(char)
        new_code = list(new_code)
        new_code.insert(int(char_index/4 + 1), char)
        newer_code = ""
        code.pop(char_index)
        for char_awesome in new_code:
            newer_code += char_awesome

try:
    print(newer_code)
except NameError:
    print(new_code)

Giving in the input of teyabcvbonhmonhmponu nbvdteyabcvbnbnfbcvb returns hellot here. I am not sure why this happens as my cipher gives the correct output with spaces, but the decipher is wonky, I tried going through the original input, making it a list, then if the character was a space, I would get it’s index, divide it by four, remove it from the original code, and then adding it to a new string, but it ends up wonky. Can anyone help?

2 Answers

Well, it was a little hard to debug because the code is confused, but here I have a simpler solution and I don't know if it works for you. Anyway I'll let it below, the explatation of every line is there and if you have some doubts just let me know :)

code = input("What text would you like to decipher: ")
words = code.split(' ') # If there's any space
string_result = ''
chars = {
    "uytv": "a",
    "ghas": "b",
    "opts": "c",
    "qwra": "d",
    "bcvb": "e",
    "wdrs": "f",
    "rwes": "g",
    "teya": "h",
    "uisd": "i",
    "hgnl": "j",
    "mvbn": "k",
    "onhm": "l",
    "ponu": "o",
    "gbho": "m",
    "wesg": "n",
    "idgf": "p",
    "asfb": "q",
    "nbnf": "r",
    "dngd": "s",
    "nbvd": "t",
    "mbnf": "u",
    "ignf": "v",
    "fddf": "w",
    "sabd": "x",
    "asda": "y",
    "qfjy": "z"
}
for word in words:
    out = [(word[i:i+4]) for i in range(0, len(word), 4)] # Split the word into 4 characters
    result = [chars[a] for a in out] # Getting the representation
    string_result += ''.join(result) + ' ' # Adding the space in each iteration

print(string_result) # Outputs "hello there"

Correct answer by Brad Figueroa on January 4, 2021

Brad offered a directly solution.Here is shorter one:

chars = {....}
code = input("What text would you like to decipher: ")
result = " ".join(["".join(chars["".join(j)] for j in zip(*[iter(i)]*4)]) for i in code.split())
print(result)

Useful link: How does zip(*[iter(s)]*n) work in Python?

Answered by jizhihaoSAMA on January 4, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP