The Kāmasūtra1 is a text written in the 4th century AD by the Brahim scholar Vātsyāyana2, but based on manuscripts dating back to the 4th century BC. Chapter 3 of Part I recommends that women should study 64 arts, including cooking, dressing, massage and the preparation of perfumes. The list also includes some less obvious arts, including conjuring, chess, bookbinding and carpentry.
Number 44 on the list is mlecchita vikalpa3, which has been translated into English as "the art of understanding writing in cypher, and the writing of words in a peculiar way". It is one of the earliest descriptions of encryption by substitution and is advocated in order to help women conceal the details of their liaisons. One of the recommended techniques involves randomly pairing letters of the alphabet, and then substituting each letter in the original message with its partner.
The Kāmasūtra cipher uses a key $$(k_1, k_2)$$ where the 26 letters of the alphabet are randomly distributed across two 13-character strings $$k_1$$ and $$k_2$$ (str), for example THEQUICKBROWN and FXJMPSVLAZYDG. If we write both parts of the key underneath each other in a table
T | H | E | Q | U | I | C | K | B | R | O | W | N |
F | X | J | M | P | S | V | L | A | Z | Y | D | G |
we get a substitution cipher that substitutes the letter T for the letter F, the letter F for letter T, the letter H for the letter X, the letter X for the letter H, and so forth. When encoding a message, uppercase letters are substituted by their corresponding uppercase letter and lowercase letters are substituted by their corresponding lowercase letter. All characters from the original message that aren't letters, are copied unchanged into the encoded message. Your task:
Write a function iskey that takes two strings $$k_1$$ and $$k_2$$ (str). The function must return a Boolean value (bool) that indicates whether $$k_1$$ and $$k_2$$ are both 13-character strings that together contain each of the 26 letters in the alphabet just once. The function may not make a distinction between uppercase and lowercase letters.
Write a function encode_character that takes a character $$c$$ (str) and two strings $$k_1$$ and $$k_2$$ (str). The function must return the encoded version (str) of character $$c$$ according to a Kāmasūtra cipher with key $$(k_1, k_2)$$.
Write a function encode that takes a message $$m_p$$ (str) and two strings $$k_1$$ and $$k_2$$ (str). The function must return the encoded version (str) of message $$m_p$$ according to a Kāmasūtra cipher with key $$(k_1, k_2)$$.
Write a function decode that takes a message $$m_c$$ (str) and two strings $$k_1$$ and $$k_2$$ (str). Message $$m_c$$ is the encoded version of a message $$m_p$$ according to a Kāmasūtra cipher with key $$(k_1, k_2)$$. The function must return message $$m_p$$ (str).
You may assume that the strings $$k_1$$ and $$k_2$$ that are passed to the functions encode_character, encode and decode form a valid key $$(k_1, k_2)$$ for the Kāmasūtra cipher, without the need to check this explicitly.
>>> iskey('THEQUICKBROWN', 'FXJMPSVLAZYDG')
True
>>> iskey('ABCDEFGHIJKLM', 'NOPQRSTUVW???')
False
>>> iskey('ABCDEFGHIJKLM', 'NOPQRSTUVW')
False
>>> encode_character('Q', 'THEQUICKBROWN', 'FXJMPSVLAZYDG')
'M'
>>> encode_character('v', 'THEQUICKBROWN', 'FXJMPSVLAZYDG')
'c'
>>> encode_character('?', 'THEQUICKBROWN', 'FXJMPSVLAZYDG')
'?'
>>> encode('A person who does nothing will enjoy no happiness.', 'THEQUICKBROWN', 'FXJMPSVLAZYDG')
'B ujziyg dxy wyji gyfxsgn dskk jgeyo gy xbuusgjii.'
>>> decode('B ujziyg dxy wyji gyfxsgn dskk jgeyo gy xbuusgjii.', 'THEQUICKBROWN', 'FXJMPSVLAZYDG')
'A person who does nothing will enjoy no happiness.'