Morse Code
April 28, 2009
Yesterday was the 218th anniversary of the birth of Samuel F. B. Morse, who invented the telegraph. We present this simple exercise in his honor.
Morse Code is an encoding of alphanumeric characters using short and long pulses of sound. Originally developed in the 1840s for the use of Morse’s telegraph, it is still in use today, primarily in the fields of amateur radio and aviation. Letters and digits consist of dots and dashes as shown in the chart at right; a single space is inserted between characters, and an extra space is inserted between words. For instance, “Programming Praxis” is rendered in Morse code as the string “• — — • • — • — — — — — • • — • • — — — — — • • — • — — • • — — • • — • • — — • • — • • • • •
“.
A • — | N — • | 1 • — — — — |
B — • • • | O — — — | 2 • • — — — |
C — • — • | P • — — • | 3 • • • — — |
D — • • | Q — — • — | 4 • • • • — |
E • | R • — • | 5 • • • • • |
F • • — • | S • • • | 6 — • • • • |
G — — • | T — | 7 — — • • • |
H • • • • | U • • — | 8 — — — • • |
I • • | V • • • — | 9 — — — — • |
J • — — — | W • — — | 0 — — — — — |
K — • — | X — • • — | |
L • — • • | Y — • — — | |
M — — | Z — — • • |
Your task is to write functions that convert back and forth between character strings and Morse code. When you are finished, you are welcome to read or run a suggested solution, or to post your solution or discuss the exercise in the comments below.