When reading out these digital seven-segment displays, the digits on the fifth row have been obscured. What are they?
They are the same as the digits in the first row. We have added colors to the segments of the digits on the two top rows, to make it easier to see that the digits on a given row count how many segments of each type (color) occur in the previous row. Remarkably, this is the only loop that already repeats itself after four steps.
As the name already suggests, a seven-segment display is a form of electronic display device that uses seven segments to render decimal digits. If all seven segments are on, they render the number 8. The segments are numbered starting from zero in the usual reading order: from left to right and from top to bottom.
By switching specific segments on or off, the ten decimal digits are rendered on a display in the following way.
Here, segments that are on are shown in red. Note that the digit 1 is rendered by only turning on the two segments on the left, not the two segments on the right. This is a detail that is important in view of what follows.
The rendering of a digit on a seven-segment display is represented as a set with the numbers (int) of the segments that are turned on. For example, the rendering of digit 3 on a seven-segment display is represented as the set $$\{0, 2, 3, 5, 6\}$$. These segments are turned on on a seven-segment display to render each of the ten decimal digits:
digit | segments |
---|---|
0 | 0, 1, 2, 4, 5, 6 |
1 | 1, 4 |
2 | 0, 2, 3, 4, 6 |
3 | 0, 2, 3, 5, 6 |
4 | 1, 2, 3, 5 |
5 | 0, 1, 3, 5, 6 |
6 | 0, 1, 3, 4, 5, 6 |
7 | 0, 2, 5 |
8 | 0, 1, 2, 3, 4, 5, 6 |
9 | 0, 1, 2, 3, 5, 6 |
A readout $$r = r_0r_1r_2r_3r_4r_5r_6$$ of seven digits on seven seven-segment displays is represented as a string (str) of the seven digits. For each readout $$r$$ we can determine the next readout $$s = s_0s_1s_2s_3s_4s_5s_6$$, where $$s_i$$ ($$i = 0, \ldots, 6$$) counts how often segment $$i$$ is on in the rendering of the digits of readout $$r$$.
Your task:
Write a function digit2segments that takes a decimal digit $$d$$ (int). If $$d$$ is not a valid decimal digit, the function must raise an AssertionError with the message invalid digit. Otherwise, the function must return the rendering of digit $$d$$ on a seven-segment display.
Write a function segments2digit that takes the rendering $$t$$ of a digit on a seven-segment display. If $$t$$ is not a valid rendering, the function must raise an AssertionError with the message invalid segments. Otherwise, the function must return the digit (int) corresponding to rendering $$t$$.
Write a function next_readout that takes a readout $$r$$. If $$r$$ is not a valid readout, the function must raise an AssertionError with the message invalid readout. Otherwise the function must return the next readout after readout $$r$$.
Write a function cycle that takes a readout $$r$$. f $$r$$ is not a valid readout, the function must raise an AssertionError with the message invalid readout. Otherwise the function must return a list that starts with readout $$r$$, followed by all subsequent readouts, until a readout is found that already appears in the list. The latter (repeated) readout must not be included in the list.
>>> digit2segments(3)
{0, 2, 3, 5, 6}
>>> digit2segments('A')
Traceback (most recent call last):
AssertionError: invalid digit
>>> segments2digit({0, 5, 2, 6, 3})
3
>>> segments2digit([2, 3, 5])
Traceback (most recent call last):
AssertionError: invalid segments
>>> next_readout('4635263')
'6447366'
>>> next_readout('6447366')
'5546374'
>>> next_readout('5546374')
'5546174'
>>> next_readout('5546174')
'4635263'
>>> cycle('ABCDEFG')
Traceback (most recent call last):
AssertionError: invalid readout
>>> cycle('4635263')
['4635263', '6447366', '5546374', '5546174']
>>> cycle('4436426')
['4436426', '4557364', '5546174', '4635263', '6447366', '5546374']
>>> cycle('ABCDEFG')
Traceback (most recent call last):
AssertionError: invalid readout