Flag semaphore is a system for conveying information at a distance that was widely used in the 19th century. The system uses two short poles with square flags, which a signal person holds in different positions to signal letters of the alphabet and digits. The signaler holds one pole in each hand, and extends each arm in one of eight possible directions. The positions are numbered from zero, starting with the position straight up and then each time rotated 45° clockwise.
A semaphore signal (or semaphore for short) is represented as string (str) with two digits between 0 and 7 (boundaries included) indicating the positions of both flags. The order of the digits makes no difference, because it does not matter which hand is used to take the positions of the flags. For example, 17 and 71 represent the same semaphore.
A semaphore alphabet consists of 28 different semaphores: 26 semaphores representing uppercase letters (A–Z), and two more semaphores representing a space and a pound sign (#). Ten semaphores representing uppercase letters (but not the uppercase letter J) also represent the 10 digits (0–9). Several semaphore alphabets exist, but internationally this is the most common alphabet.
When interpreting successive semaphores, we can either be in letter mode or in digit mode. When signaling a message, we always start in letter mode. In letter mode, we use the uppercase letter for semaphores representing two symbols (an uppercase letter and a digit). The semaphore representing the pound sign (#) means that we should switch to digit mode (so the semaphore itself does not represent a symbol in the message). In digit mode, we use the digit for semaphores representing two symbols (an uppercase letter and a digit). When in digit mode, the semaphore representing the uppercase letter J means that we should switch back to letter mode (and in that case the semaphore itself does not represent a symbol in the message). For semaphores representing a single symbol (an uppercase letter or a space, but not J or #), it makes no difference whether we are in letter mode or in digit mode.
We represent the display of an epoch on a 24-hour digital clock as a string (str) in the format hh:mm, where the hours $$h$$ ($$0 \leq u < 24$$) and the minutes $$m$$ ($$0 \leq m < 60$$) are always represented with two digits by adding leading zeros where needed. For example 13:50 or 07:03.
If an epoch is displayed on an analog clock, we can interpret the positions of the hour hand and the minute hand as a semaphore. The angles $$\alpha_h$$ and $$\alpha_m$$ (in degrees) made by the hour and minute hands can be calculated as \[ \begin{align} \alpha_h &= 30\,(h\!\!\!\!\!\mod{12}) + \frac{m}{2} \\ \alpha_m &= 6\,m \end{align} \] Then we use this formula to determine the semaphore position $$p_\alpha$$ that corresponds to the angle $$\alpha$$ (in degrees) of the hand of an analog clock: \[ p_\alpha = \left[\frac{\alpha}{45}\right]\!\!\!\!\!\mod{8} \] where $$\left[x\right]$$ represents the rounding of $$x \in \mathbb{R}$$ to the nearest integer.
We work with text files that describe a semaphore alphabet. Such a file consists of 28 lines, each of which consists of one of the two possible representations of a semaphore, followed by a tab and one or two symbols represented by the semaphore. For semaphores that represent two symbols, the uppercase letter always precedes the digit. For example, these are some lines from the most comaon semaphore alphabet (alphabet.txt1):
54 A1 64 B2 74 C3 04 D4 14 E5 … 15 L 25 M 35 N 67 O 60 P … 12 W 13 X 23 Z 44 01 #
Note that semaphore 44 represents a space in this alphabet, so the penultimate line of the file has a space after the tab.
Your task is to interpret a sequence of epochs such as
17:55 01:59 22:29 03:03 21:01 12:08 00:38
as a message transmitted by semaphore signals. Below you can see how this sample sequence of epochs can be interpreted as the message C3P02.
Note that according to this semaphore alphabet, semaphore 06 only represents the uppercase letter P (and no digit), so it was not strictly necessary to switch back to letter mode between the transmission of the digits 3 and 0 (transmitted in digit mode). So we could also have transmitted C3P0 with this sequence of epochs
17:55 01:59 22:29 21:01 00:38
Your task:
Write a function semaphore_alphabet that takes the location (str) of a text file describing a semaphore alphabet. The function must return the dictionary representation of the semaphore alphabet. This is a dictionary (dict) that maps each representation (str) of each semaphore in the file onto a string (str) with all symbols represented by that semaphore. If a semaphore has two representations, both representations must occur as keys in the dictionary. If a semaphore represents two symbols, they must keep their order from the text file.
Write a function epoch2semaphore that takes an epoch $$e$$ (str). The function must return a representation (str) of the semaphore corresponding to epoch $$e$$, whose first digit indicates the position of the hour hand and whose second digit indicates the position of the minute hand.
Write a function semaphore2epochs that takes the representation (str) of a semaphore $$s$$. The function must return a set containing all epochs corresponding to a representation of semaphore $$s$$ (taking the two possible representations of the semaphore into consideration).
Write a function epoch2symbol that takes an epoch $$e$$ (str) and the dictionary representation $$\mathcal{A}$$ of a semaphore alphabet. The function also has a third optional parameter digit_mode that may take a Boolean value (bool; default value: False). If semaphore alphabet $$\mathcal{A}$$ has no symbols that correspond with epoch $$e$$$, an AssertionError must be raised with message invalid epoch. Otherwise, the function must return the symbol (str) that corresponds with epoch $$e$$ according to semaphore alphabet $$\mathcal{A}$$ in letter mode (digit_mode=False) or in digit mode (digit_mode=True).
Write a function clock_reading that takes two arguments: i) a sequence of epochs (str) that are separated by spaces and ii) the dictionary representation $$\mathcal{A}$$ of a semaphore alphabet. The function must return the message (str) that is obtained when interpreting the sequence of epochs as semaphores according to semaphore alphabet $$\mathcal{A}$$. If the sequence contains an epoch that cannot be interpreted as a symbol according to semaphore alphabet $$\mathcal{A}$$, an AssertionError must be raised with message invalid epoch.
In the following interactive session we assume the text file alphabet.txt3 is located in the current directory.
>>> alphabet = semaphore_alphabet('alphabet.txt4')
>>> alphabet['24']
'F6'
>>> alphabet['42']
'F6'
>>> alphabet['26']
'R'
>>> alphabet['44']
' '
>>> alphabet['10']
'#'
>>> alphabet['37']
Traceback (most recent call last):
KeyError: '37'
>>> epoch2semaphore('12:43')
'06'
>>> epoch2semaphore('05:17')
'42'
>>> epoch2semaphore('04:45')
'36'
>>> semaphore2epochs('06')
{'00:42', '00:43', '00:44', '00:45', '08:57', '08:58', '08:59', '09:00', '09:01', '09:02', '09:03', '11:42', '11:43', '11:44', '11:45', '11:46', '11:47', '11:48', '12:42', '12:43', '12:44', '12:45', '20:57', '20:58', '20:59', '21:00', '21:01', '21:02', '21:03', '23:42', '23:43', '23:44', '23:45', '23:46', '23:47', '23:48'}
>>> semaphore2epochs('36')
{'03:46', '03:47', '03:48', '04:42', '04:43', '04:44', '04:45', '04:46', '04:47', '04:48', '08:19', '08:20', '08:21', '08:22', '08:23', '08:24', '08:25', '08:26', '09:19', '09:20', '09:21', '09:22', '09:23', '09:24', '09:25', '09:26', '15:46', '15:47', '15:48', '16:42', '16:43', '16:44', '16:45', '16:46', '16:47', '16:48', '20:19', '20:20', '20:21', '20:22', '20:23', '20:24', '20:25', '20:26', '21:19', '21:20', '21:21', '21:22', '21:23', '21:24', '21:25', '21:26'}
>>> semaphore2epochs('42')
{'02:27', '02:28', '02:29', '02:30', '02:31', '02:32', '02:33', '03:27', '03:28', '03:29', '03:30', '03:31', '03:32', '03:33', '05:15', '05:16', '05:17', '05:18', '06:12', '06:13', '06:14', '06:15', '06:16', '06:17', '06:18', '14:27', '14:28', '14:29', '14:30', '14:31', '14:32', '14:33', '15:27', '15:28', '15:29', '15:30', '15:31', '15:32', '15:33', '17:15', '17:16', '17:17', '17:18', '18:12', '18:13', '18:14', '18:15', '18:16', '18:17', '18:18'}
>>> epoch2symbol('22:30', alphabet)
'C'
>>> epoch2symbol('12:07', alphabet)
'#'
>>> epoch2symbol('10:30', alphabet, digit_mode=True)
'3'
>>> epoch2symbol('12:15', alphabet, digit_mode=True)
'J'
>>> epoch2symbol('9:03', alphabet)
'P'
>>> epoch2symbol('12:08', alphabet)
'#'
>>> epoch2symbol('12:37', alphabet, digit_mode=True)
'0'
>>> epoch2symbol('15:13', alphabet)
Traceback (most recent call last):
AssertionError: invalid epoch
>>> clock_reading('17:55 01:59 22:29 03:03 21:01 12:08 00:38', alphabet)
'C3P0'
>>> clock_reading('17:55 01:59 22:29 21:01 00:38', alphabet)
'C3P0'
>>> clock_reading('10:02 22:37 14:41 13:30 18:28 19:56 16:46 17:32 19:16 22:47 19:25 06:06 23:12', alphabet)
'TIME IS MONEY'
>>> clock_reading('11:51 19:54 07:17 18:06 05:28 06:49 16:49 18:27 18:43 11:04 04:39 11:36', alphabet)
Traceback (most recent call last):
AssertionError: invalid epoch
The international peace symbol (☮) was designed in 1958 by British designer and artist Gerald Holtom5. It is a circle with a vertical line in the center from which two forking lines run to either side. The symbol is a super-imposition of the semaphore signals for the letters N and D, taken to stand for nuclear disarmament.