Home > Net >  Having trouble debugging a function that should translate Morse code to English
Having trouble debugging a function that should translate Morse code to English

Time:11-06

Having trouble with some classwork I can't seem to get the function to return the proper word.

I am expecting to get "hi there!" but I keep getting "eeeeee teeeeeaee!".

This is the hint they provide:

The problem is that the Morse symbols are translated in the wrong order.

For example, '.' which means 'e' is translated before '....' which means 'h'.

As a result, '....' is not translated as 'h' but as 'eeee'.

Hence to fix the translator, one needs to first translate all 4-character symbols, then all three-character symbols, then all two-character symbols, and at the end the two one-character symbols '.' and '-'.

The delimiters should be done at the end - first '||' should be replaced with one white space ' ', and then the single delimiter '|' with the empty text string ' '.

def morse2txt(t):
    """Translates Morse code to text"""
    
    t = t.replace(".-", 'a')
    t = t.replace("-...", 'b')
    t = t.replace("-.-.", 'c')
    t = t.replace("-..", 'd')
    t = t.replace(".", 'e')
    t = t.replace("..-.", 'f')
    t = t.replace("--.", 'g')
    t = t.replace("....", 'h')
    t = t.replace("..", 'i')
    t = t.replace(".---", 'j')
    t = t.replace("-.-", 'k')
    t = t.replace(".-..", 'l')
    t = t.replace("--", 'm')
    t = t.replace("-.", 'n')
    t = t.replace("---", 'o')
    t = t.replace(".--.", 'p')
    t = t.replace("--.-", 'q')
    t = t.replace(".-.", 'r')
    t = t.replace("...", 's')
    t = t.replace("-", 't')
    t = t.replace("..-", 'u')
    t = t.replace("...-", 'v')
    t = t.replace(".--", 'w')
    t = t.replace("-..-", 'x')
    t = t.replace("-.--", 'y')
    t = t.replace("--..", 'z')
    t = t.replace("||", ' ')
    t = t.replace("|", '')
    
    return t

# Main program 

morse = "....|..||-|....|.|.-.|.|!"
txt=morse2txt(morse)
print(txt)

CodePudding user response:

You need to do what the hint says:

Hence to fix the translator, one needs to first translate all 4-character symbols, then all three-character symbols, then all two-character symbols, and at the end the two one-character symbols '.' and '-'.

So, that would look something like this:

def morse2txt(t):
    #First, translate the 4-character symbols
    t = t.replace("-...", 'b')
    t = t.replace("-.-.", 'c')
    t = t.replace("-..", 'd')
    t = t.replace("..-.", 'f')
    t = t.replace("....", 'h')
    t = t.replace(".---", 'j')
    t = t.replace(".-..", 'l')
    t = t.replace(".--.", 'p')
    t = t.replace("--.-", 'q')
    t = t.replace("...-", 'v')
    t = t.replace("-..-", 'x')
    t = t.replace("-.--", 'y')
    t = t.replace("--..", 'z')

    #Next, translate the 3-character symbols
    t = t.replace("-.-", 'k')
    t = t.replace("---", 'o')
    t = t.replace(".-.", 'r')
    t = t.replace("...", 's')
    t = t.replace("..-", 'u')
    t = t.replace(".--", 'w')

    #Then, translate the 2-character symbols
    t = t.replace(".-", 'a')
    t = t.replace("-.", 'n')
    t = t.replace("--.", 'g')
    t = t.replace("..", 'i')
    t = t.replace("--", 'm')

    #After that, translate the 1-character symbols
    t = t.replace("-", 't')
    t = t.replace(".", 'e')

    #Finally, translate the delimiters
    t = t.replace("||", ' ')
    t = t.replace("|", '')

    return t

morse = "....|..||-|....|.|.-.|.|!"

txt = morse2txt(morse)

print(txt)

Translating first the 4-character symbols (ones like -...), then the 3-character symbols (ones like -.-), then the 2-character symbols (ones like .-), then the 1-character symbols (- and .), and finally the delimiters (|| and |). If you don't do it this way, and instead do the 1-character symbols before any of the other symbols, they will break them. For example, in what you tried 4 e's replaced a h because e is . and h is ...., and if you do e first it will make 4 e's instead of a h. The same problem will occur if you do the 2-symbols before the 3-symbols or 4-symbols, or the 3-symbols before the 4-symbols.

  • Related