To convert a message into unreadable gibberish, triple rotation can be used. The triple rotation uses three integers $$k_1$$, $$k_2$$ and $$k_3$$ which are the key to the cipher. To encrypt a given message, the characters of the message are divided into three groups: the letters a to i form the first group (both uppercase and lowercase), the letters j to r the second group (uppercase and lowercase), and all other characters (including numbers, spaces and punctuation) form the third group. Within group $$i$$ ($$i = 1, 2, 3$$) all characters thereafter are shifted $$k_i$$ positions to the left, with the characters that drop out on the left being placed on the right. Every group is thus rotated independently from the other two groups. The decoding then takes place on the basis of a rotation with $$k_i$$ positions to the right within each group $$i$$ ($$i = 1, 2, 3$$).
If, for example, we want to encrypt the message
Nobody expects the Spanish Inquisition!
with the triple rotation with $$k_1 = 2$$, $$k_2 = 3$$ en $$k_3 = 1$$. For this message group 1 is formed with the letters b, d, e, e, c, h, e, a, i, h, I, i, i, i which occur at positions 3, 5, 8, 11, 12, 17, 18, 22, 24, 26, 28, 32, 34, 36 in the message. We move all letters of this group $$k_1 = 2$$ positions to the left, with the letters b and d,which fall out, being placed at the end of the sequence. For group 1 this results in the new series of letters e, e, c, h, e, a, i, h, I, i, i, i, b, d, which are then put back in the original positions of group 1. This step of the procedure is illustrated in the figure below.
When this procedure is executed for group 2 with $$k_2 = 3$$ and for group 3 with $$k_3 = 1$$, then the encrypted message reads as follows
ppene xctnhes t aiSsqhoI iuinNsitb!dooy
That looks completely nonsensical.
Write a function encode that can be used to encode a given a sentence according to the principle of triple rotation. Four arguments must be passed to this function: a sentence (str), and the values $$k_1$$, $$k_2$$ and $$k_3$$ (int) that must be used for the triple rotation. The function must return the encoded sentence (str) as a result.
Write a function decode that can be used for decode a sentence that was encrypted according to the principle of triple rotation. Four arguments must be passed to this function: a given encoded sentence (str) and values $$k_1$$, $$k_2$$ and $$k_3$$ (int), which were used for the triple rotation. The function must return the original sentence (str) as a result.
>>> encode('Nobody expects the Spanish Inquisition!', 2, 3, 1)
'ppene xctnhes t aiSsqhoI iuinNsitb!dooy'
>>> decode('ppene xctnhes t aiSsqhoI iuinNsitb!dooy', 2, 3, 1)
'Nobody expects the Spanish Inquisition!'