26 lines
1002 B
Python
26 lines
1002 B
Python
# Enter a sentence containing only letters of the alphabet and numbers 0-9 and output the morse code equivalent.
|
|
|
|
class MorseCode
|
|
morse_code: (text) ->
|
|
morse_code_map =
|
|
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: '--..'
|
|
'0': '-----', '1': '.----', '2': '..---', '3': '...--'
|
|
'4': '....-', '5': '.....', '6': '-....', '7': '--...'
|
|
'8': '---..', '9': '----.'
|
|
result = []
|
|
for char in text.toLowerCase()
|
|
if morse_code_map[char]?
|
|
result.push morse_code_map[char]
|
|
else if char is ' '
|
|
result.push ' / '
|
|
result.join ''
|
|
|
|
s = new MorseCode()
|
|
console.log s.morse_code "To be or not to be"
|
|
|
|
|