A straddling checkerboard is a type of cipher code that is used to convert an alphabet to numbers. At the same time, there is a certain type of data compression ingrained in this method. For this, a grid (straddling checkerboard) of the following type is used:
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | |
---|---|---|---|---|---|---|---|---|---|---|
E | T | A | O | N | R | I | S | |||
2 | B | C | D | F | G | H | J | K | L | M |
6 | P | Q | _ | U | V | W | X | Y | Z | . |
The first row has ten digits in ascending order (0-9). The second line is usually filled with high-frequency letters. Here, two options are left open. Furthermore, this row isn't preceded by a label. The two following rows are labeled with numbers that weren't appointed a letter in the second row, and are filled with the remaining letters of the alphabet.
The symbols of the alphabet can be placed in the grid randomly. The grid has a total of 30 slots, two of which are not being used in the second row. If we use an alphabet of 26 letters, two extra slots can be filled with other symbols. In the example above, we chose a dot (.) and an underscore (_).
When coding, a letter in the second row is replaced by the number that is used as the column label. Letters in the third and fourth row are replaced by a two-digit number, the first of which is the row label and the second is the column label. Because high frequency letters occur on the second row, the length of the coded message can be kept to a minimum. The message THE_SHAWSHANK_REDEMPTION, for example, can be coded using the grid above:
T | H | E | _ | S | H | A | W | S | H | A | N | K | _ | R | E | D | E | M | P | T | I | O | N |
1 | 25 | 0 | 62 | 9 | 25 | 3 | 65 | 9 | 25 | 3 | 5 | 27 | 62 | 7 | 0 | 22 | 0 | 29 | 60 | 1 | 8 | 4 | 5 |
Decoding is simply done by reversing the process. In spite of the fact that the groups can exist of both 1 and 2 digits, the decoding happens unambiguously because a group of 2 digits always begins with a number that wasn't appointed to a letter in the second row of the grid (2 and 6 in the example above). All other numbers form a group of 1 digit.
Implement the four functions below that are used to code and decode messages using the straddling checkerboard method. To each of these functions, four arguments should be given, the last three of which consist of ten characters and correspond to the second, third and fourth row of the grid used. The second string argument contains two spaces, that indicate the position of the letters in the second row.
A function letter2digits to which as first argument, should be given a character that occurs in the grid given (i.e. in one of the other string arguments). The function has to return the string that consists of one or two digits that correspond with the character given, using the straddling checkerboard method.
A function digits2letter to which, as a first argument, a string consisting of one or two digits should be given. The function must return the character from the given grid that corresponds to the two digits using the straddling checkerboard method.
A function code to which, as first argument, should be given a string that consists solely of characters from the given grid. The function has to return the coded string (consisting of numbers) that is made using the straddling checkerboard.
A function decode to which, a first argument, should be given a string that consists solely of numbers. The function should return the decoded string that is made using the straddling checkerboard method.
>>> letter2digits('A', 'ET AON RIS', 'BCDFGHJKLM', 'PQ_UVWXYZ.')
'3'
>>> letter2digits('C', 'ET AON RIS', 'BCDFGHJKLM', 'PQ_UVWXYZ.')
'21'
>>> letter2digits('U', 'ET AON RIS', 'BCDFGHJKLM', 'PQ_UVWXYZ.')
'63'
>>> digits2letter('3', 'ET AON RIS', 'BCDFGHJKLM', 'PQ_UVWXYZ.')
'A'
>>> digits2letter('21', 'ET AON RIS', 'BCDFGHJKLM', 'PQ_UVWXYZ.')
'C'
>>> digits2letter('63', 'ET AON RIS', 'BCDFGHJKLM', 'PQ_UVWXYZ.')
'U'
>>> code('THE_SHAWSHANK_REDEMPTION', 'ET AON RIS', 'BCDFGHJKLM', 'PQ_UVWXYZ.')
'1250629253659253527627022029601845'
>>> decode('1250629253659253527627022029601845', 'ET AON RIS', 'BCDFGHJKLM', 'PQ_UVWXYZ.')
'THE_SHAWSHANK_REDEMPTION'