a friend posted a sentence in Morse code, and rather than looking up a translation directly, I decided to write a script to translate it. I’m going to clean this up and post it over in my code samples when it’s prettier and has more functions, and I’m hesitant to post a rough draft of something so basic, but in the interest of accountability to myself, here’s a (functional) early version anyway, followed by the tests to ensure it works:
# morse code tranlsator
# dict of Morse code
morseCode = { "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" : "--..", "one" : ".----", "two" : "..---", "three" : "...--",
"four" : "....-", "five" : ".....", "six" : "-....", "seven" : "--...",
"eight" : "---..", "nine" : "----.", "zero" : "-----", " " : " " }
# alphanum-to-morse conversion
def alphaInput(secretMsg):
translatedWords = []
words = secretMsg.lower().split(' ')
for word in words:
tempChars = []
for c in word:
m = morseCode.get(c)
tempChars.append(m)
tempWord = ' '.join(tempChars)
translatedWords.append(tempWord)
return ' '.join(translatedWords)
# TODO: morse-to-alphanum
# TODO: swap "dot" and "dash"
def morseInput(secretMsg):
translatedWords = []
morseWordList = secretMsg.split('\n')
# Look up Morse-word line by line; one row = one word
for word in morseWordList:
chars = []
chars = word.split(' ') # splits each Morse-word-row into Morse-characters
translatedChars=[]
for c in chars:
chardict = { k:v for k,v in morseCode.items() if v == c }
char = list(chardict.keys())[0]
translatedChars.append(char)
word = ''.join(translatedChars)
translatedWords.append(word)
return ' '.join(translatedWords)
# TODO: input / output (can django do this?)
# should this be in another file?
secretMsg = "... --- -- . --- -. .\n-.- -. --- .--\n-- --- .-. ... .\n-.-. --- -.. ."
# TODO: FIX THIS so it knows when to translate inputs written out as dot/dash
# the comparators with 'dot' and 'dash' don't work
if ((secretMsg[0] in '.-') or (secretMsg[0:3] == 'dot') or (secretMsg[0:4] == 'dash')):
translated = morseInput(secretMsg)
else:
translated = alphaInput(secretMsg)
print(translated)
and the tests:
import unittest, morse
class MorseTest(unittest.TestCase):
def test_alpha_to_morse(self):
self.assertEqual(morse.alphaInput('A'), '.-')
self.assertEqual(morse.alphaInput('SOS'), '... --- ...')
self.assertEqual(morse.alphaInput('This is a test'), '- .... .. ... .. ... .- - . ... -')
self.assertEqual(morse.alphaInput('Cats are awesome'), '-.-. .- - ... .- .-. . .- .-- . ... --- -- .')
def test_morse_to_alpha(self):
self.assertEqual(morse.morseInput('... --- ...'), 'sos')
self.assertEqual(morse.morseInput('- .... .. ...\n.. ...\n.-\n- . ... -'), 'this is a test')
self.assertEqual(morse.morseInput('-.-. .- - ...\n.- .-. .\n.- .-- . ... --- -- .'), 'cats are awesome')