The keyboard cipher uses a specific keyboard layout. For example, let's use the most common keyboard layout in Belgium: AZERTY. In this layout the digits and letters are arranged over four rows in the following way:

AZERTY
AZERTY keyboard layout used in Belgium.

In each keyboard layout, the 10 digits and 26 uppercase letters each appear on exactly one key. There are no other keys. The keys are arranged over a number of rows, indexed from top to bottom starting from 1. Each row contains a maximum of ten keys, indexed from left to right starting from 1. However, if there are ten keys in a row, the tenth key is indexed as 0. That way, the column index of each key consists of one digit and we can represent the position of each key as a pair $$(r, c)$$, with $$r$$ the row index and $$c$$ the column index of the key.

When encoding a message, only the letters and digits in the message are retained. The other characters are ignored. There's no distinction between uppercase and lowercase letters. The ciphertext consists of a number of lines, the same number as there are rows in the keyboard layout used. For each letter or digit in the message, the column index (one digit) of the corresponding key is on the line that corresponds to the row where the key is located. The other lines contain a hyphen (-) in the corresponding column. For example, if we encode the message

The first computer was invented in the 1940s.

with an AZERTY keyboard layout, we get an ciphertext that consists of four lines

THEFIRSTCOMPUTERWASINVENTEDINTHE1940S
    
--------------------------------1940-
5-3-84-5-9-07534-1-8--3-53-8-5-3-----
-6-4--2---0-------2-------3---6-----2
--------3-------1---64-6----6--------

As a reference, we have added the consecutive letters and digits from the message on top. The first letter of the message, the uppercase letter T, is found at position $$(2, 5)$$ on the keyboard. Therefore, the second line of the ciphertext starts with a 5 and the other lines start with a hyphen (-). The second letter of the message, the letter h, is found at position $$(3, 6)$$ on the keyboard. Therefore, the second character on the third line of the ciphertext is a 6 and the second character on the other lines is a hyphen (-).

Assignment

We represent a keyboard layout as a sequence (list or tuple) of rows on the keyboard (listed from top to bottom). Each row is represented as a string (str) containing the digits and letters on the keys of that row (listed from left to right). Each row contains at least one and at most ten keys. The 10 digits and 26 uppercase letters are each on exactly one key and there are not other keys.

Your task:

The last four functions may assume that only valid arguments are passed, without having to check this explicitly.

Example

In the following interactive session we assume the text file message.txt1 to be located in the current directory.

>>> azerty = ['1234567890', 'AZERTYUIOP', 'QSDFGHJKLM', 'WXCVBN']
>>> islayout(azerty)
True
>>> position('T', azerty)
(2, 5)
>>> position('m', azerty)
(3, 0)
>>> key(2, 5, azerty)
'T'
>>> key(3, 0, azerty)
'M'
>>> print(encode('The first computer was invented in the 1940s.', azerty))
--------------------------------1940-
5-3-84-5-9-07534-1-8--3-53-8-5-3-----
-6-4--2---0-------2-------3---6-----2
--------3-------1---64-6----6--------
>>> decode('message.txt2', azerty)
'THEFIRSTCOMPUTERWASINVENTEDINTHE1940S'