Morse code is a method for encrypted communication of textual information that gets transmitted as a series of on-off tones, lights or clicks at certain intervals. Fixed combinations of these signals represent the different letters, punctuation marks and digits. This Morse code was invented and developed in 1835 by Samuel Morse for use in telegraph systems. Using an electrical telegraph (see figure) it was possible to choose between two states: key down (= electric current) or key up (= no electric current) and duration (short or long). Telegraphy is generally considered to be the forerunner of later digital communication. It should be noted that during speed communication competitions between skilled Morse code operators and experts in sending text messages, Morse code generally wins1.

telegraaf

In the International Morse Code, short signals are denoted as dots (.) and long signals as dashes (-). Each character is then represented by a unique sequence of dots and dashes. As such, the letter S is represented in Morse code as ... and the letter O as ---. For emergency signals, Morse code can be sent by way of improvised sources that can be easily "keyed" on and off, making it one of the simplest and most versatile methods of telecommunication. The most common distress signal is SOS or three dots, three dashes and three dots, internationally recognized by treaty.

Assignment

The text file morse.txt2 contains a list of characters (first column) and their corresponding representation in Morse code (second columns). Both columns are separated by a tab. When you inspect this file, you will see for example that the letter C is represented in Morse code as -.-. and a comma is represented as --..--.

When transmitting messages in Morse code, a short break (letter space) is usually inserted in between two successive characters. Such a short break is denoted by a space character. In between words, a longer break (word space) is inserted, which is denoted by a forward slash (/). As such, the word EARN is represented as

. .- .-. -.

and the word URN as

..- .-. -.

However, if we ignore the breaks (spaces), these two words are represented by the same pattern in Morse code. Words represented by the same Morse code pattern are called isomorse. Also note that the two words given as an example are homophones: they are pronounced the same but differ in meaning and spelling. Your task is to:

Example

In the following interactive session, we assume that the text files morse.txt3 and words.txt4 are located in the current directory.

>>> codes = morsecodes('morse.txt')
>>> codes['C']
'-.-.'
>>> codes['U']
'..-'
>>> codes['N']
'-.'

>>> pattern('TAIPAN', codes)
'-.-...--..--.'
>>> pattern('TAIPAN', codes, complement=True)
'.-.---..--..-'
>>> pattern('TAIPAN', codes, mirror=True)
'.--..--...-.-'
>>> pattern('TAIPAN', codes, complement=True, mirror=True)
'-..--..---.-.'

>>> isomorse('TAIPAN', 'CUNETTE', codes)
True
>>> isomorse('TAIPAN', 'PYTHON', codes)
False
>>> isomorse('TAIPAN', 'ROUX', codes, complement=True)
True
>>> isomorse('TAIPAN', 'PATIENT', codes, mirror=True)
True
>>> isomorse('TAIPAN', 'TUDOR', codes, complement=True, mirror=True)
True
>>> isomorse('EDOMITE', 'EDOMITE', codes, mirror=True)
True

>>> groups('words.txt', 'morse.txt')
{'.--...-...-..': {'WILL', 'PITIED', 'PERINE', 'ADENINE'}, '.--.-.---....----.': {'PYTHON'}, '-.-...--..--.': {'TREPAN', 'CUNETTE', 'TAIPAN'}}

Epilogue

The American inventor and businessman Thomas Alva Edison (1847-1931) took it a bit far: he proposed his wife in Morse code and nicknamed his children Marion Estelle en Thomas Alva Jr. as Dot and Dash.