Coded Correspondence

Hello,

I have tried everything so far and I cannot understand why its not working after the first two words.

coded_message = "dfc aruw fsti gr vjtwhr wznj? vmph otis! cbx swv jipreneo uhllj kpi rahjib eg fjdkwkedhmp!"
keyword = "friends"
alphabet = "abcdefghijklmnopqrstuvwxyz"


def vigenere_decoder(coded, keyword):
    keyword_only_phrase = []
    counter = 0
    for letter in coded:
        if letter in alphabet:
            if counter != len(keyword):
                keyword_only_phrase.append(keyword[counter])
                counter += 1
            else:
                counter = 0
        else:
            keyword_only_phrase.append(letter)
    #print(keyword_only_phrase)
    return ''.join(keyword_only_phrase)
                

def shifter_decoder(coded, keyword):
    keyword_only_phrase = vigenere_decoder(coded, keyword)
    decoded_message = []
    counter = 0
    for letter in keyword_only_phrase:
        if letter in alphabet:
            index = alphabet.find(coded[counter]) - alphabet.find(letter)
            decoded_message.append(alphabet[index])
            counter += 1
        else:
            decoded_message.append(letter)
            counter += 1
    return ''.join(decoded_message)
    
print(vigenere_decoder(coded_message, keyword))
print(shifter_decoder(coded_message, keyword))

this yields:

fri ends fri en dsfri ends? fri ends! fr ien dsfrien dsfr ien dsfri en dsfriends!
you were abl vt wdeco nmth? uin luww! ni uxk peqibec bvzx mdy ghfya nn gqwiwcmcr!

Thank you very much in advance :smiley:

What I know is that it’s mostly the problem with the first function. As you can see the first two words are correct.

redo your decoding for vigenere_decoder to be more like what you have for shifter_decoder now.
the problem is here:

                keyword_only_phrase.append(keyword[counter])

Also, you’ll need to do the decoding in that inner else statement as well.
(You can’t just skip the letter when counter is 0.)

Here’s a version using much of the code given above.
(spoiler below)

possible answer below:

coded_message = "dfc aruw fsti gr vjtwhr wznj? vmph otis! cbx swv jipreneo uhllj kpi rahjib eg fjdkwkedhmp!" keyword = "friends" alphabet = "abcdefghijklmnopqrstuvwxyz" def vigenere_decoder(coded, keyword): keyword_only_phrase = [] counter = 0 for letter in coded: if letter in alphabet: if counter != len(keyword): letter_index = (alphabet.find(letter) - alphabet.find(keyword[counter])) % len(alphabet) keyword_only_phrase.append(alphabet[letter_index]) counter += 1 else: counter = 0 letter_index = (alphabet.find(letter) - alphabet.find(keyword[counter])) % len(alphabet) keyword_only_phrase.append(alphabet[letter_index]) counter += 1 else: keyword_only_phrase.append(letter) #print(keyword_only_phrase) return ''.join(keyword_only_phrase) print(vigenere_decoder(coded_message, keyword))
1 Like

Oh, I see it now, wow. Thank you so much, this does make more sense. You also reset the counter then you started adding to it in the same conditional action block.

Do you think there is something fundamentally wrong with the way my logic was thought out to solve the problem?

most of the logic was okay;
you just forgot that there had to be stuff done even when the counter was reset to zero,
and that you have to do it in the same iteration, otherwise it would go to the next letter (meaning a letter would have been skipped).
The loops you used and where you put the if and else (and the conditions used there) was done well.
Using a counter in the loop (to keep track of what index to use in the keyword) was a great idea too.

Your decoding (- just one or two lines) was wrong in vigenere_decoder, although you had more of the code correct for that decoding in shifter_decoder.
Much of that had to be redone (and also adjusted for possibly going off the end of the alphabet using %).
Try to do that in smaller pieces … it doesn’t all have to be done on one line, (or even just two lines like I did). Try to figure out the logic in smaller steps.