In Morse code1, each character of a word is converted into a unique combination of dots (.) and dashes (-). The conversions of individual characters are separated by spaces. However, if we remove all spaces from the Morse code, we can come up with some new definitions.
We say that words are isomorse if they have the same Morse code. For example, the following three words are isomorse:
word | Morse code |
---|---|
ADMITTED | .- -.. -- .. - - . -.. |
EMIGATED | . -- .. --. .- - . -.. |
PANACE | .--. .- -. .- -.-. . |
If we swap all dots and dashes in the Morse code of a word (dots become dashes and dashes become dots) and thus obtain the Morse code of another word, we say that these words are antimorse.
word | Morse code | swapped Morse code | word |
---|---|---|---|
DECATHLON | -.. . -.-. .- - .... .-.. --- -. | .- --. -.-- .. --- -- .- -. .. .- | AGYIOMANIA |
EMERITUS | . -- . .-. .. - ..- ... | -. .- -. --- .-- .- - - | NANOWATT |
NOSTALGIA | -. --- ... - .- .-.. --. .. .- | .-.. .- -- . -. - .- - .. --- -. | LAMENTATION |
TORMENTERS | - --- .-. -- . -. - . .-. ... | . ... -.-. .- .-. --. --- - | ESCARGOT |
VERTEBRAL | ...- . .-. - . -... .-. .- .-.. | -- -.-- . -.-. - --- .--. -.-- | MYECTOPY |
If we reverse the order of the dots and dashes in the Morse code of a word and thus obtain the Morse code of another word, we say that these words are revomorse.
word | Morse code | reversed Morse code | word |
---|---|---|---|
DISCLAIM | -.. .. ... -.-. .-.. .- .. -- | -- .. -. . .-. .- .-.. .. ... - | MINERALIST |
FRACTURE | ..-. .-. .- -.-. - ..- .-. . | .. -. - . .-. - .-- .. -. . -.. | INTERTWINED |
INTENSION | .. -. - . -. ... .. --- -. | .- -- -... .. . -. -.-. . | AMBIENCE |
REVIEWER | .-. . ...- .. . .-- . .-. | .- ..- -.. .. - .. ...- . | AUDITIVE |
WAVELETS | .-- .- ...- . .-.. . - ... | ... - .. .-. .-. ..- .--. | STIRRUP |
If the Morse code of a word is palindromic (reverse Morse code is the same as the Morse code itself) then we say that the word is a palinmorse.
word | Morse code |
---|---|
DIATOMIST | -.. .. .- - --- -- .. ... - |
INTERNAL | .. -. - . .-. -. .- .-.. |
PROTECTORATE | .--. .-. --- - . -.-. - --- .-. .- - . |
RESEARCHER | .-. . ... . .- .-. -.-. .... . .-. |
WINTERTIME | .-- .. -. - . .-. - .. -- . |
Of course we may also combine the forgoing, swapping all dots and dashes as well as reversing the order of dots and dashes.
To determine the Morse code representation of words, the conversion of each character onto its corresponding unique combination of dots and dashes is recorded in a text file. Each line of that file consists of a character, followed by a space and the unique combination of dots and dashes for that character. Because conversion into Morse code makes no distinction between uppercase and lowercase characters, letters are always represented in uppercase in the text file. Your task:
Write a function morse_code that takes the location (str) of a text file containing the conversion from characters onto their unique combination of dots and dashes. The function must return a dictionary (dict) that maps each character (str) in the given file onto its corresponding combination of dots and dashes (str).
Write a function text2morse that takes two arguments: i) a word $$w$$ (str) and ii) a dictionary $$d$$ (dict) that maps characters (str) onto their corresponding combination of dots and dashes (str). The function must return the Morse code of $$w$$ (including spaces between the conversions of the individual characters). No distinction may be made between uppercase and lowercase. On the other hand, the function may assume that all characters in $$w$$ appear as keys in $$d$$, without the need to check this explicitly.
Write a function swap that takes a string $$s$$ (str). The function must return $$s$$ in which all occurrences of dots (.) and dashes (-) are swapped: dots are replaced by dashes, dashes are replaced by dots, but all other characters remain unchanged.
Write a function database that takes two arguments: i) the location (str) of a text file containing a list of words (each on a separate line) and ii) a dictionary $$d$$ (dict) that maps characters (str) into their corresponding combination of dots and dashes (str). The function must return a database for the given list of words. This is a dictionary (dict) whose keys are the Morse code of all words in the given file, without spaces. The dictionary must map each key onto the set of all words (str) in the file having that Morse code representation (without spaces). The database thus groups isomorse words from the given file.
Write a function lookup that takes three arguments: i) a word $$w$$ (str), ii) a dictionary $$d$$ (dict) that maps characters (str) into their corresponding combination of dots and dashes (str) and iii) a database (dict) for a given list of words. The function must return a set containing all words (str) from the database that have the same Morse code as $$w$$.
The function also has an optional parameter swapped that takes a Boolean value (bool, default value: False). If the value True is passed to the parameter swapped, the words must be looked up in the database with all occurrences of dots (.) and dashes (-) swapped in the Morse code.
The function also has an optional parameter reversed that takes a Boolean value (bool, default value: False). If the value True is passed to the parameter reversed, the words must be looked up in the database with dots and dashes in the Morse code in reverse order.
In the following interactive session we assume the text files morse.txt2 and words.txt3 to be located in the current directory.
>>> morse = morse_code('morse.txt4')
>>> morse['A']
'.-'
>>> morse['P']
'.--.'
>>> morse['?']
'..--..'
>>> text2morse('ADMITTED', morse)
'.- -.. -- .. - - . -..'
>>> text2morse('emigated', morse)
'. -- .. --. .- - . -..'
>>> text2morse('OBSESSIVE', morse)
'--- -... ... . ... ... .. ...- .'
>>> swap('.- -.. -- .. - - . -..')
'-. .-- .. -- . . - .--'
>>> swap('--- -... ... . ... ... .. ...- .')
'... .--- --- - --- --- -- ---. -'
>>> words = database('words.txt5', morse)
>>> words['.--..--..--.-..']
{'ADMITTED', 'EMIGATED', 'PANACE'}
>>> lookup('ADMITTED', morse, words)
{'ADMITTED', 'EMIGATED', 'PANACE'}
>>> lookup('NOSTALGIA', morse, words, swapped=True)
{'LAMENTATION'}
>>> lookup('DISCLAIM', morse, words, reversed=True)
{'GENERALIST', 'MINERALIST'}
>>> lookup('WINTERTIME', morse, words, reversed=True)
{'WINTERTIME'}
>>> lookup('NOTATION', morse, words, swapped=True, reversed=True)
{'DUDDER', 'BADDER', 'DUNLIN', 'BEGLUE'}
>>> lookup('GASTROSCOPE', morse, words, reversed=True, swapped=True)
{'GASTROSCOPE'}
Fittingly, the English word OBSESSIVE has a long run of dots (18) in its Morse code:
--- -... ... . ... ... .. ...- .
However, the record holder is LESSEESHIP with a run of 21 dots in its Morse code:
.-.. . ... ... . . ... .... .. .--.