Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why my code does not decode the encrypted string based on the dictionary?

I have a dictionary with keys and values that represent letters.

for example a simple one :

DICT_CODE = {'b' : 'g', 'n' :'a', 'p' : 'o', 'x' : 'd', 't' : 'y'}

I've received an encrypted code and turned the string into a list, where each item is a word. I need to solve it, according to the items in the dictionary.

an example for a code is :

words_list = ["bppx","xnt!"]  # "good day!"

I've tried to solve it by using double for loops, as here:

 for word in words_list:
     for char in word:
         if char in string.letters:
            word = word.replace(char, DICT_CODE.get(char))
 print words_list

expected output -> ["good","day!"]

output -> ["bppx","xnt!"]

It does not working at all. the charcaters stay the same and the code is stil unknown. I don't understand why it isn't working, if someone has time to look and try to help me and see whats wrong, or even suggest a better way (that works).

like image 445
Maya Avatar asked May 13 '26 03:05

Maya


2 Answers

Changing the word variable inside the for loop, would not change the string inside the word_list. You would need to remember the index and update the element at that index (and get the word from the index) -

for i, word in enumerate(words_list):
    for char in word:
            if char in string.letters:
                    words_list[i] = words_list[i].replace(char, DICT_CODE.get(char))

Demo -

>>> words_list = ["bppx","xnt!"]
>>> DICT_CODE = {'b' : 'g', 'n' :'a', 'p' : 'o', 'x' : 'd', 't' : 'y'}
>>> for i, word in enumerate(words_list):
...     for char in word:
...             if char in string.letters:
...                     words_list[i] = words_list[i].replace(char, DICT_CODE.get(char))
>>> words_list
['good', 'day!']

But an easier way for you would be to use str.translate (along with string.maketrans ). Example -

table = string.maketrans('bnpxt','gaody') #First argument characters in your original string, and second argument what they map to.
for i, word in enumerate(words_list):
    words_list[i] = word.translate(table)

Demo -

>>> import string
>>> table = string.maketrans('bnpxt','gaody')  #This creates the translation table
>>> words_list = ["bppx","xnt!"]
>>> for i, word in enumerate(words_list):
...     words_list[i] = word.translate(table)
... 
>>> print words_list
['good', 'day!']

This using list comprehension -

words_list[:] = [word.translate(table) for word in words_list]

Demo -

>>> words_list = ["bppx","xnt!"]
>>> table = string.maketrans('bnpxt','gaody')
>>> words_list[:] = [word.translate(table) for word in words_list]
>>> words_list
['good', 'day!']
like image 178
Anand S Kumar Avatar answered May 15 '26 16:05

Anand S Kumar


Your problem is that you don't actually modify original list.

for i, word in enumerate(words_list):
    for char in word:
        if char in string.letters:
            word = word.replace(char, DICT_CODE.get(char))
            words_list[i] = word

print words_list

['good', 'day!']
like image 28
xiº Avatar answered May 15 '26 17:05

xiº



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!