-
-
Save ebuckley/1842461 to your computer and use it in GitHub Desktop.
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 |
Same as above for punctuation:
"." : ".-.-.-",
"," : "--..--",
":" : "---...",
"?" : "..--..",
"'" : ".----.",
"-" : "-....-",
"/" : "-..-.",
"@" : ".--.-.",
"=" : "-...-"
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!
This code will only work if the final item in the submitted string is a whitespace, otherwise it will return None.
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
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?
Same for me Glenderman
You can extend your morse alphabet by including the numbers as well: