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.
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.
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:
Write a function morsecodes that takes the location of a text file. Each line of this file must contain a character, followed by a tab and the representation of the character in Morse code. The function must return a dictionary that maps each character in the given file onto its representation in Morse code. In case the character is a letter, the mapping must use its uppercase variant as a key.
Write a function pattern that takes two arguments: a word and a dictionary that maps characters onto their corresponding representation in Morse code. You may assume that each character in the given word occurs as a key in the given dictionary, with mapping of letters making use of their uppercase variant. The function has two additional parameters complement and mirror, that both take a Boolean value (default value: False). The function must compose the pattern that represents the given word in Morse code (without intermediate spaces in between representations of successive characters). Next, the function must determine the complement of the composed pattern by replacing each dot by a dash and vice versa, but only if the value True is passed to the parameter complement. Next, the function must mirror the pattern by reversing the order of its characters, but only if the value True is passed to the parameter mirror. The pattern obtained in this way must be returned as the result of the function.
Write a function isomorse that takes three arguments: two words and a dictionary that maps characters onto their corresponding representation in Morse code. You may assume that all characters in the given words are used as keys in the given dictionary, with mapping of letters making use of their uppercase variant. In addition, the function has two optional parameters complement and mirror that take Boolean values (default value: False). The function must return a Boolean value that indicates whether the two given words are isomorse. This is the case if both words are represented by the same pattern in Morse code. In order to determine the pattern in Morse code for a given word, the function must make use of the given dictionary and the function pattern. In comparing the two given words, the pattern of the second word may have to be complemented and/or mirrored in case the value True is passed respectively to the parameters complement and mirror.
Write a function groups that takes the location of two text files. The first file must contain a list of words, each on a separate line. Each line in the second file must contain a character, followed by a tab and the representation of that character in Morse code. You may assume that all characters in the given words also occur in the second file. The function must return a dictionary that groups the given words that are represented by the same pattern in Morse code, by mapping each pattern in Morse code that corresponds to a given word onto the set of all words that are represented by that pattern. To make it clear: this function never needs to compute complemented or mirrored patterns.
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'}}
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.