pythondecodingcaesar-cipherlogic-error

Decoding the Ceasar Cipher Ends Up with Logic Error


I'm a complete beginner when it comes to coding, I was training in CodeWars when I came across this question:

The soldiers of a country use ceasar cipher for messaging. Only letters are changed and everything else is not touched in these messages (like "!" and space). Each message is split into 4 or 5 parts and each part is carried by one runner. The first part starts with 2 letters: The first one shows a random letter and the second one is the same letter that has been changed by shifting (so that the soldiers know the number of the shift). Now write a code for decoding and encoding the message I should have known that you would have a perfect answer for me!!!

The encoding function works. However, the following function that I wrote for decoding does not. I enter the "ijJ tipvme ibw", "f lopxo uibu z", "pv xpvme ibwf ", "b qfsgfdu botx", "fs gps nf!!!" message and I'm supposed to get I should have known that you would have a perfect answer for me!!!, but instead I get some nonsense message saying 'ž~ ¨¤ª¡™ –«', ' "š £¤¬£ ©–© ®', ' "¤ª ¬¤ª¡™ –«š ', ' "– ¥š§›š˜© –£¨¬', ' "š§ ›¤§ ¢š!!!

I import re first:

import re

Here's the function:

def decode(arr):
    arr = str(arr) #turn what we have into str
    array = list(re.split('",', arr)) #get rid of the ' ", ' between every part so that
                                # it will be a single whole messege in the end and also turn it into
                                #a list in which every item is a single part

    decoded_messege = ""

    first_messege = list(array[0]) #turns the first part into a list of letters
    letter_shift = ord(first_messege[1]) - ord(first_messege[0]) #realizes the number of shift

    for messege in array:
        for letter in messege:
            if (ord(letter) > 122 or ord(letter) < 65): #the letter is not an alphabet (a space or !)
                decoded_messege += letter

            else: #the letter is an alphabet
                decoded_messege += chr(ord(letter) - letter_shift) #the ascii code is the current ascii number
                                                                #minus the shift

    decoded_message = decoded_messege[2:] #ignore the first two letters that are
                                        #only for showing shift and don't matter in the message
    return decoded_messege

Solution

  • The problem is that you're taking into account the first two characters to calculate the shift: "-->", which is 0. Change it to arr = str(arr)[2:] Also, the return should be at the same level of the first loop.

    import re
    
    
    def decode(arr):
        arr = str(arr)[2:] #turn what we have into str
        array = list(re.split('",', arr)) #get rid of the ' ", ' between every part so that
                                        # it will be a single whole messege in the end and also turn it into
                                        #a list in which every item is a single part
    
        decoded_messege = ""
    
        first_messege = list(array[0]) #turns the first part into a list of letters
        letter_shift = ord(first_messege[1]) - ord(first_messege[0]) #realizes the number of shift
    
        for messege in array:
            for letter in messege:
                if letter != '"':
                    # print(letter)
                    if (ord(letter) > 122 or ord(letter) < 65): #the letter is not an alphabet (a space or !)
                        decoded_messege += letter
                    else: #the letter is an alphabet
                        decoded_messege += chr(ord(letter) - letter_shift) #the ascii code is the current ascii number                                                                    #minus the shift
            decoded_message = decoded_messege[2:] #ignore the first two letters that are
                                                #only for showing shift and don't matter in the message
        return decoded_messege
    
    print(decode('""ijJ tipvme ibw", "f lopxo uibu z", "pv xpvme ibwf ", "b qfsgfdu botx", "fs gps nf!!!""'))
    # hiI should hav e known that y ou would have  a perfect answ er for me!!!
    ```