Fixed errors in morse.
Updated the parsing logic.
Test cases improved to test more strictly.
Code updated to naming and stype conventions.
DOT = '.' DASH = '-' SPACE = ' ' MORSE = { ' ': [SPACE], 'a': [DOT, DASH], 'b': [DASH, DOT, DOT, DOT], 'c': [DASH, DOT, DASH, DOT], 'd': [DASH, DOT, DOT], 'e': [DOT], 'f': [DOT, DOT, DASH, DOT], 'g': [DASH, DASH, DOT], 'h': [DOT, DOT, DOT, DOT], 'i': [DOT, DOT], 'j': [DOT, DASH, DASH, DASH], 'k': [DASH, DOT, DASH], 'l': [DOT, DASH, DOT, DOT], 'm': [DASH, DASH], 'n': [DASH, DOT], 'o': [DASH, DASH, DASH], 'p': [DOT, DASH, DASH, DOT], 'q': [DASH, DASH, DOT, DASH], 'r': [DOT, DASH, DOT], 's': [DOT, DOT, DOT], 't': [DASH], 'u': [DOT, DOT, DASH], 'v': [DOT, DOT, DOT, DASH], 'w': [DOT, DASH, DASH], 'x': [DASH, DOT, DOT, DASH], 'y': [DASH, DOT, DASH, DASH], 'z': [DASH, DASH, DOT, DOT], '0': [DASH, DASH, DASH, DASH, DASH], '1': [DOT, DASH, DASH, DASH, DASH], '2': [DOT, DOT, DASH, DASH, DASH], '3': [DOT, DOT, DOT, DASH, DASH], '4': [DOT, DOT, DOT, DOT, DASH], '5': [DOT, DOT, DOT, DOT, DOT], '6': [DASH, DOT, DOT, DOT, DOT], '7': [DASH, DASH, DOT, DOT, DOT], '8': [DASH, DASH, DASH, DOT, DOT], '9': [DASH, DASH, DASH, DASH, DOT], } def morse_code(msg): return ''.join(map(lambda letter: ''.join(MORSE[letter]), msg.lower()))
dot='.'dash='-'morse = {':space:':[' '],'a':[dot,dash],'b':[dash,dot,dot,dot],'c':[dash,dot,dash,dot],'d':[dash,dot,dot],'e':[dot],'f':[dot,dot,dash,dot],'g':[dash,dash,dot],'h':[dot,dot,dot,dot],""'i':[dot,dot],'j':[dot,dash,dash,dash],'k':[dash,dot,dash],'l':[dot,dash,dot,dot],'m':[dash,dash],'n':[dash,dot],'o':[dash,dash,dash],'p':[dot,dash,dash],'q':[dash,dash,dot,dash],""'r':[dot,dash,dot],'s':[dot,dot,dot],'t':[dash],'u':[dot,dot,dash],'v':[dot,dot,dot,dash],'w':[dot,dash,dash],'x':[dash,dot,dot,dash],'y':[dash,dot,dash,dash],'z':[dash,dot,dot],""'0':[dash,dash,dash,dash,dash],'1':[dot,dash,dash,dash,dash],'2':[dot,dot,dash,dash,dash],'3':[dot,dot,dot,dash,dash],'4':[dot,dot,dot,dot,dash],'5':[dot,dot,dot,dot,dot],""'6':[dash,dot,dot,dot,dot],'7':[dash,dash,dot,dot,dot],'8':[dash,dash,dash,dot,dot],'9':[dash,dash,dash,dash,dot]}- DOT = '.'
- DASH = '-'
- SPACE = ' '
- MORSE = {
- ' ': [SPACE],
- 'a': [DOT, DASH],
- 'b': [DASH, DOT, DOT, DOT],
- 'c': [DASH, DOT, DASH, DOT],
- 'd': [DASH, DOT, DOT],
- 'e': [DOT],
- 'f': [DOT, DOT, DASH, DOT],
- 'g': [DASH, DASH, DOT],
- 'h': [DOT, DOT, DOT, DOT],
- 'i': [DOT, DOT],
- 'j': [DOT, DASH, DASH, DASH],
- 'k': [DASH, DOT, DASH],
- 'l': [DOT, DASH, DOT, DOT],
- 'm': [DASH, DASH],
- 'n': [DASH, DOT],
- 'o': [DASH, DASH, DASH],
- 'p': [DOT, DASH, DASH, DOT],
- 'q': [DASH, DASH, DOT, DASH],
- 'r': [DOT, DASH, DOT],
- 's': [DOT, DOT, DOT],
- 't': [DASH],
- 'u': [DOT, DOT, DASH],
- 'v': [DOT, DOT, DOT, DASH],
- 'w': [DOT, DASH, DASH],
- 'x': [DASH, DOT, DOT, DASH],
- 'y': [DASH, DOT, DASH, DASH],
- 'z': [DASH, DASH, DOT, DOT],
- '0': [DASH, DASH, DASH, DASH, DASH],
- '1': [DOT, DASH, DASH, DASH, DASH],
- '2': [DOT, DOT, DASH, DASH, DASH],
- '3': [DOT, DOT, DOT, DASH, DASH],
- '4': [DOT, DOT, DOT, DOT, DASH],
- '5': [DOT, DOT, DOT, DOT, DOT],
- '6': [DASH, DOT, DOT, DOT, DOT],
- '7': [DASH, DASH, DOT, DOT, DOT],
- '8': [DASH, DASH, DASH, DOT, DOT],
- '9': [DASH, DASH, DASH, DASH, DOT],
- }
- def morse_code(msg):
return ''.join(list(map(lambda i:''.join(morse[i.lower().replace(' ', ':space:')]),msg)))print(morse_code('11 2 11'))- return ''.join(map(lambda letter: ''.join(MORSE[letter]), msg.lower()))
# Simple. test.assert_equals(morse_code('sos'),'...---...') # Spaces. test.assert_equals(morse_code('hello world'),'......-...-..--- .-----.-..-..-..') # Capital letters. test.assert_equals(morse_code('YOU PASSED'),'-.-----..- .--..-.......-..') # All digits. test.assert_equals(morse_code('0123456789'),'-----.----..---...--....-.....-....--...---..----.') # All letters. test.assert_equals(morse_code('The quick brown fox jumps over the lazy dog'),'-..... --.-..-..-.-.-.- -....-.---.---. ..-.----..- .---..---.--.... ---...-..-. -..... .-...---..-.-- -..-----.')
# TODO: Replace examples and use TDD development by writing your own tests# These are some of the methods available:# test.expect(boolean, [optional] message)# test.assert_equals(actual, expected, [optional] message)# test.assert_not_equals(actual, expected, [optional] message)# You can use Test.describe and Test.it to write BDD style test groupings- # Simple.
- test.assert_equals(morse_code('sos'),'...---...')
- # Spaces.
- test.assert_equals(morse_code('hello world'),'......-...-..--- .-----.-..-..-..')
test.assert_equals(morse_code('you passed'),'-.-----..- .--.-.......-..')test.assert_equals(morse_code('is a good day'),'..... .- --.-------.. -...--.--')- # Capital letters.
- test.assert_equals(morse_code('YOU PASSED'),'-.-----..- .--..-.......-..')
- # All digits.
- test.assert_equals(morse_code('0123456789'),'-----.----..---...--....-.....-....--...---..----.')
- # All letters.
- test.assert_equals(morse_code('The quick brown fox jumps over the lazy dog'),'-..... --.-..-..-.-.-.- -....-.---.---. ..-.----..- .---..---.--.... ---...-..-. -..... .-...---..-.-- -..-----.')