Pollux is a cipher that works in two steps. A message is encoded by first converting it into Morse: a code that was used to send messages via a telegraph. A message in Morse consists of a sequence of short signals (represented by a dot (.)) and long signals (represented by a dash (-)). Each character is represented as a unique combination of short and long signals. A short pause is inserted between two successive characters, which is represented as a space. This way, the word CASTOR in Morse is represented as
-.-. .- ... - --- .-.
For the second step of the encoding we determine which characters can replace the Morse symbols (dots, dashes and spaces). The same character can never be used to replace multiple symbols. Each symbol of the message in Morse is then replaced by a randomly selected character from the characters that can replace the symbol. Suppose, for example, that we determine that Morse symbols can be replaced by the following characters:
dot (.) | 0378AEFMOPQXYZ |
dash (-) | 145BCGJNRTW |
space ( ) | 269DHIKLSUV |
The Pollux cipher can then encode the word CASTOR in the following way:
plaintext | C A S T O R |
Morse | -.-. .- ... - --- .-. |
ciphertext | 4PGM9F1VQEMHCUB1B28BP |
We give another example, and now determine that Morse symbols can be replaced by the following characters:
dot (.) | 158 |
dash (-) | 047 |
space ( ) | 2369 |
The Pollux cipher can then encode the message BETA GEMINORUM in the following way:
plaintext | B E T A G E M I N O R U M |
Morse | -... . - .- --. . -- .. -. --- .-. ..- -- |
ciphertext | 78559590317270898307655205347035412184970 |
Note that the space in BETA GEMINORUM has no representation in Morse code, and is therefore not converted. Decoding a Pollux ciphertext is done in reverse order: all characters from the ciphertext are converted back to their corresponding Morse symbols, and the resulting message in Morse is converted back to the original message.
We specify a Morse code in a text file. Each line of the file starts with a unique character, followed by a tab and the unique combination of dots and dashes that represent the character in Morse. As an example, we will work with the text file morse.txt1:
A .- B -... C -.-. D -.. E . ⋮ ⋮ ( -.--. = -...- @ .--.-.
Your task:
Write a function read_code that takes the location (str) of a text file specifying a Morse code. The function must return a dictionary (dict) that maps each character (str) from the file onto the corresponding sequence of dots and dashes (str). This is called the dictionary representation of the Morse code.
Write a function text2morse that takes two arguments: i) a message (str) and ii) the dictionary representation of a Morse code $$\mathcal{M}$$ (dict). The function must return the message in Morse, using Morse code $$\mathcal{M}$$ for the conversion. All characters of the message that have no representation in Morse code $$\mathcal{M}$$ are ignored during conversion into Morse.
Write a function morse2text that takes two arguments: i) a message in Morse (str) and ii) the dictionary representation of the Morse code (dict) used. The function must return the original message (str).
Write a function encode that takes five arguments. The first argument is a message (str) and the second argument the dictionary representation of a Morse code $$\mathcal{M}$$ (dict). The last three arguments are strings (str) that respectively determine which characters can replace dots, dashes and spaces. The function must return the Pollux ciphertext of the message using Morse code $$\mathcal{M}$$. Make sure that each dot, dash and space is replaced completely at random.
Write a function decode that takes five arguments. The first argument is the Pollux ciphertext of a message (str). The second argument is the dictionary representation of the Morse code (dict) used. The last three arguments are strings (str) that respectively determine which characters can replace dots, dashes and spaces. The function must return the original message (str).
The following interactive session assumes the text file morse.txt2 is located in the current directory.
>>> code = read_code('morse.txt3')
>>> code['A']
'.-'
>>> code['8']
'---..'
>>> text2morse('CASTOR', code)
'-.-. .- ... - --- .-.'
>>> morse2text('-.-. .- ... - --- .-.', code)
'CASTOR'
>>> text2morse('BETA GEMINORUM', code)
'-... . - .- --. . -- .. -. --- .-. ..- --'
>>> morse2text('-... . - .- --. . -- .. -. --- .-. ..- --', code)
'BETAGEMINORUM'
>>> encode('CASTOR', code, '0378AEFMOPQXYZ', '145BCGJNRTW', '269DHIKLSUV')
'57TEUANUPXA9GUC14IQJ3'
>>> decode('4PGM9F1VQEMHCUB1B28BP', code, '0378AEFMOPQXYZ', '145BCGJNRTW', '269DHIKLSUV')
'CASTOR'
>>> encode('BETA GEMINORUM', code, '158', '047', '2369')
'01589134657940131904651341200468056880907'
>>> decode('78559590317270898307655205347035412184970', code, '158', '047', '2369')
'BETAGEMINORUM'
Pollux — designated β Geminorum (Latinised to Beta Geminorum; abbreviated as Beta Gem or β Gem) — is an orange-hued evolved giant star about 34 light-years of the Sun in the constellation of Gemini4. It is the brightest star in Gemini and the closest giant star to the Sun.
The name of the star refers to the twins Castor and Pollux5 in Greek and Roman mythology.