Skip to content

Instantly share code, notes, and snippets.

@ebuckley
Created February 16, 2012 05:55
Show Gist options
  • Save ebuckley/1842461 to your computer and use it in GitHub Desktop.
Save ebuckley/1842461 to your computer and use it in GitHub Desktop.
python code to encode/decode morse code
morseAlphabet ={
"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" : "--..",
" " : "/"
}
inverseMorseAlphabet=dict((v,k) for (k,v) in morseAlphabet.items())
testCode = ".... . .-.. .-.. --- / -.. .- .. .-.. -.-- / .--. .-. --- --. .-. .- -- -- . .-. / --. --- --- -.. / .-.. ..- -.-. -.- / --- -. / - .... . / -.-. .... .- .-.. .-.. . -. --. . ... / - --- -.. .- -.-- "
# parse a morse code string positionInString is the starting point for decoding
def decodeMorse(code, positionInString = 0):
if positionInString < len(code):
morseLetter = ""
for key,char in enumerate(code[positionInString:]):
if char == " ":
positionInString = key + positionInString + 1
letter = inverseMorseAlphabet[morseLetter]
return letter + decodeMorse(code, positionInString)
else:
morseLetter += char
else:
return ""
#encode a message in morse code, spaces between words are represented by '/'
def encodeToMorse(message):
encodedMessage = ""
for char in message[:]:
encodedMessage += morseAlphabet[char.upper()] + " "
return encodedMessage
@tentpegbob
Copy link

You can extend your morse alphabet by including the numbers as well:

"1" : ".----",
"2" : "..---",
"3" : "...--",
"4" : "....-",
"5" : ".....",
"6" : "-....",
"7" : "--...",
"8" : "---..",
"9" : "----.",
"0" : "-----"

@attilathedud
Copy link

Same as above for punctuation:

    "." : ".-.-.-",
    "," : "--..--",
    ":" : "---...",
    "?" : "..--..",
    "'" : ".----.",
    "-" : "-....-",
    "/" : "-..-.",
    "@" : ".--.-.",
    "=" : "-...-"

@Chinmaytare
Copy link

Chinmaytare commented Apr 3, 2017

Hello everyone,
I'm new to python and I am unable to understand the 'decodeMorse' function:
`def decodeMorse(code, positionInString = 0):

    if positionInString < len(code):
        morseLetter = ""
        for key,char in enumerate(code[positionInString:]):
            if char == " ":
                positionInString = key + positionInString + 1
                letter = inverseMorseAlphabet[morseLetter]
                return letter + decodeMorse(code, positionInString)
            
            else:
                morseLetter += char
    else:
        return ""
    `

Also, why do we use this:
testCode = ".... . .-.. .-.. --- / -.. .- .. .-.. -.-- / .--. .-. --- --. .-. .- -- -- . .-. / --. --- --- -.. / .-.. ..- -.-. -.- / --- -. / - .... . / -.-. .... .- .-.. .-.. . -. --. . ... / - --- -.. .- -.-- "

Thanks in advance!

@developerjake
Copy link

This code will only work if the final item in the submitted string is a whitespace, otherwise it will return None.

@dcdeve
Copy link

dcdeve commented Sep 18, 2017

here is an example, with numbers, signs and a different function for decodeMorse

https://gist.github.com/dcdeve/3dfba6566029f87b01aa3e38d6e1e26b

My function for decodeMorse is

def decodeMorse(message):
    messageSeparated = message.split(' ')
    decodeMessage = ''
    for char in messageSeparated:
        if char in inverseMorseAlphabet:
            decodeMessage += inverseMorseAlphabet[char]
        else:
            # CNF = Character not found
            decodeMessage += '<CNF>'
    return decodeMessage

Copy link

ghost commented Nov 25, 2017

Am I being an idiot or something, cause when I copy and paste this code to test it nothing happens, am I supposed to change something?

@clarerocks2007
Copy link

Same for me Glenderman

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment