Morse code in python

Hello, I’m stuck for a while at this code:

translation_dict = dict([('a', '.-'), ('b', '-...'), ('c', '-.-.'), ('d', '-..'), ('e', '.'), ('f', '..-.'), ('g', '--.'), ('h', '....'), ('i', '..'), ('j', '.---'), ('k', '-.-'), ('l', '.-..'), ('m', '--'), ('n', '-.'), ('o', '---'), ('p', '.--.'), ('q', '--.-'), ('r', '.-.'), ('s', '...'), ('t', '-'), ('u', '..-'), ('v', '...-'), ('w', '.--'), ('x', '-..-'), ('y', '-.--'), ('z', '--..'), ('//', ''), ('-', '-....-'), ('.', '.-.-.-'), (',', '--..--'), (' ', ''), ("1", ".----"), ("2", "..---"), ("3", "...--"), ("4", "....-"), ("5", "....."), ("6", "-...."), ("7", "--..."), ("8", "---.."), ("9", "----."), ("0", "-----")])

alphabet = {}

for key, value in translation_dict.items():
    alphabet[key] = value
    alphabet[value] = key


def encode(plaintext: str) -> str:
    plaintext = plaintext.lower()
    morse_text = ""
    for char in plaintext:
        morse_text += alphabet[char] + "/"
    morse_text += "//"
    return morse_text

def decode(morse_text: str) -> str:
    morse_array = morse_text.split("/")
    plain_text = ""
    for i in range(len(morse_array)):
        current_element = morse_array[i]
        if current_element in alphabet:
            plain_text += alphabet[current_element]
    return plain_text

print(decode(encode("One, two and three pigs")))

My problem is that when I try to decode my already encoded code I get this:
on.-.-.-, -…-wo and -…-hr.-.-.-.-.-.- pigs
I figured out that letter E is in morse code: ’ . ’ so my program translate ’ . ’ as ’ .-.-.- ’ and not as E.

What should I do about it? Thanks for any answers :slight_smile:

1 Like

Hi @little2birds, and welcome to the community!

One of the problems you’re running into is how you’re storing the alphabet and morse code items into the same dictionary. When you’ve created this dictionary with values as keys and keys as values, you have multiple of the same of the same property name i.e. the period “.” and the morse code for e are both a single “.”. That creates some issues when doing a lookup on that dictionary.

An easy way to get around this, using the same programming paradigm you’re using of splitting that dictionary, is to split it into two separate simple look ups:

alphabet = {}
morse_code = {}

for key, value in translation_dict.items():
    alphabet[key] = value
    morse_code[value] = key

And then inside of the decode function, use the morse_code dictionary instead of the alphabet dictionary:

def decode(morse_text: str) -> str:
    morse_array = morse_text.split("/")
    plain_text = ""
    for i in range(len(morse_array)):
        current_element = morse_array[i]
        # swap alphabet here and below for morse_code reference
        if current_element in morse_code:
            plain_text += morse_code[current_element]
    return plain_text

And then your decoding works!

1 Like

Thank you so much, it works perfectly now :smiling_face_with_three_hearts:

1 Like

see Python&Math

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.