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.

Assignment

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:

Example

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'

Epilogue

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.

Gemini
Position of Pollux in the constellation of Gemini.

The name of the star refers to the twins Castor and Pollux5 in Greek and Roman mythology.