The tap code (sometimes called knock code) is a very simple way to encode text messages on a letter-by-letter basis. The message is transmitted using a series of tap sounds, hence its name. The code has been commonly used by prisoners to communicate with each other, usually by tapping either the metal bars, pipes or the walls inside a cell.

The code is based on a square $$5 \times 5$$ grid in which all letters are written in alphabetic order from top to bottom and from left to right, except for the letter K which is equated to the letter C.

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

The listener only needs to discriminate the timing of the taps to isolate letters, since each letter is communicated by tapping two numbers:

For example, to specify the letter B, one taps once, pauses, and then taps twice. To communicate the word VICTOR, the cipher would be the following (the pause between each number in a pair is smaller than the pause between letters):

V I C T O R
5, 1 2, 4 1, 3 4, 4 3, 4 4, 2
..... . .. ..... . ... .... .... ... .... .... ..

Because of the difficulty and length of time required for specifying a single letter, prisoners often devised abbreviations and acronyms for common items or phrases, such as GN for good night or GBU for god bless you.

By comparison, Morse code1 is harder to send by tapping or banging because it requires the ability either to create taps with a clear and precise rhythm or to produce two different-sounding noises. Morse code also takes longer to learn. Learning the square grid simply requires one to know the alphabet and the short sequence AFLQV (the initial letter of each row).

Assignment

Communication through tap code is represented as a string (str), with taps represented as dots (.) and pauses between two sequences of taps (both the pause between two numbers representing a letter and the pause between two letters) represented as spaces. Your task:

Example

>>> encode_letter('V')
(5, 1)
>>> encode_letter('i')
(2, 4)
>>> encode('VICTOR')
'..... . .. .... . ... .... .... ... .... .... ..'
>>> encode('Charlie')
'. ... .. ... . . .... .. ... . .. .... . .....'

>>> decode_letter(5, 1)
'V'
>>> decode_letter(2, 4)
'I'
>>> decode('..... . .. .... . ... .... .... ... .... .... ..')
'VICTOR'
>>> decode('. ... .. ... . . .... .. ... . .. .... . .....')
'CHARLIE'

Epilogue

United States prisoners of war (POW) during the Vietnam War are most known for having used the tap code. It was introduced in June 1965 by four POWs held in the Hỏa Lò2 prison (better known as Hanoi Hilton): Captain Carlyle "Smitty" Harris, Lieutenant Phillip Butler, Lieutenant Robert Peel, and Lieutenant Commander Robert Shumaker. Harris had heard of the tap code being used by prisoners in World War II and remembered a United States Air Force instructor who had discussed it as well.

In Vietnam, the tap code became a very successful way for otherwise isolated prisoners to communicate. POWs would use the tap code in order to communicate to each other between cells in a way which the guards would be unable to pick up on. They used it to communicate everything from what questions interrogators were asking (in order for everyone to stay consistent with a deceptive story), to who was hurt and needed others to donate meager food rations. It was easy to teach and newly arrived prisoners became fluent in it within a few days. It was even used when prisoners were sitting next to each other but not allowed to talk, by tapping on another's thigh. By overcoming isolation with the tap code, prisoners were said to be able to maintain a chain of command and keep up morale.