The Polybius method is a type of cipher code that was invented by the Greek historian and philosopher Polybius. This method can especially be used to represent a given text as a smaller number of characters. To do so, a rectangle grid is used that is filled with the characters of a given alphabet, written from left to right and from top to bottom in the cells of the grids. This alphabet is called the long alphabet. The number of characters in this alphabet must be a complete square. The characters of the second alphabet are written next to the rows (from top to bottom) and above the columns (from left to right) of the square grid. This alphabet is called the short alphabet. The square of the number of characters in the short alphabet must be equal to the number of characters in the long alphabet. Scientific characters may occur in both the long and short alphabet.

  1 2 3 4 5
1 A B C D E
2 F G H I K
3 L M N O P
4 Q R S T U
5 V W X Y Z

1 2 3 4 5 6
1 A B C D E F
2 G H I J K L
3 M N O P Q R
4 S T U V W X
5 Y Z 0 1 2 3
6 4 5 6 7 8 9

S Q U A R E
S A B C D E F
Q G H I J K L
U M N O P Q R
A S T U V W X
R Y Z   . , ?
E ! ; : " & @

The Polybius square can then be used to code a message of which all characters occur in the long alphabet. Every character of the message is converted to two characters of the short alphabet that respectively are next to the row and above the column in which the character of the message occurs in the grid. Decoding a message is done by interpreting both consecutive characters of the decoded message (that consists solely of characters from the short alphabet) as the label of respectively the row and column in which the corresponding character (from the long alphabet) is situated.

Assignment

Implement the four functions below that can be used to code and decode messages according to the Polybius method. To each of these functions, three string should be given. The second and third argument respectively represent the short and long alphabet that are used to build the Polybius square. You may always assume that the length of the long alphabet is equal to the square of the length of the short alphabet. The same character never occurs more than once within an alphabet.

Example

>>> long2short('P', '12345', 'ABCDEFGHIKLMNOPQRSTUVWXYZ')
'35'
>>> long2short('P', '123456', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')
'34'
>>> long2short('P', 'SQUARE', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ .,?!;:"&@')
'UA'

>>> short2long('35', '12345', 'ABCDEFGHIKLMNOPQRSTUVWXYZ')
'P'
>>> short2long('34', '123456', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')
'P'
>>> short2long('UA', 'SQUARE', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ .,?!;:"&@')
'P'

>>> code('POLYBIUS', '12345', 'ABCDEFGHIKLMNOPQRSTUVWXYZ')
'3534315412244543'
>>> code('POLYBIUS', '123456', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')
'3433265112234341'
>>> code('POLYBIUS VIERKANT', 'SQUARE', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ .,?!;:"&@')
'UAUUQERSSQQUAUASRUAAQUSRUEQRSSUQAQ'

>>> decode('3534315412244543', '12345', 'ABCDEFGHIKLMNOPQRSTUVWXYZ')
'POLYBIUS'
>>> decode('3433265112234341', '123456', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')
'POLYBIUS'
>>> decode('UAUUQERSSQQUAUASRUAAQUSRUEQRSSUQAQ', 'SQUARE', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ .,?!;:"&@')
'POLYBIUS VIERKANT'