Consider a string that contains all of the 26 letters of the alphabet just once, for example
jfbqpwcvuamozhilgrxtkndesy
This permutation can be used as the key in a substitution cipher, which in case of the above example maps the letter a onto the letter j, the letter b on the letter f, and so on. This key therefore encrypts the word bakery as fjmprs. Notice that the letters of the encrypted word fjmprs are in alphabetic order. The challenge is to find the permutation of letters of the alphabet, that for a given word list maximizes the number of words that are encrypted as words having their letters in alphabetic order.
Your task is to write a program that can be used to determine the score of the above optimization problem for a given key and a given list of words. The score is computed as the number of words in the given list that is encrypted as words having their letters in alphabetic order. In order to do so, you have to implement the following functions:
Write a function validKey that takes a single argument. The function must return a boolean that indicates whether or not the passed argument is a valid key for a substitution cipher. The argument is valid if it is a string that contains a permutation of the (lowercase) letters of the alphabet.
Write a function encode that can be used to encrypt a given word (first argument) according to a substitution cipher using a given key (second argument). The function must return the encrypted word as its result. If the key that is passed to the function is invalid, the function must throw an AssertionError with the message invalid key.
Write a function hasAlphabeticOrder to which a word that only contains lowercase letters must be passed as a string argument. The function must return a Boolean value that indicates whether or not the letters of the word are in alphabetic order.
Write a function score that indicates how many words contained in a given text file have their letters in alphabetic order, after they are encrypted with a substitution cipher. Two arguments must be passed to the function: the key that is used in the substitution cipher, and the name of a text file that contains the list of words. In this file, each word is on a separate line and only consists of lowercase letters. If the key passed to the function is not valid, the function must throw an AssertionError with the message invalid key.
In the following interactive session we assume the current directory to contain the text file six-letter-words.txt1.
>>> validKey('jfbqpwcvuamozhilgrxtkndesy')
True
>>> validKey('jfbqpwcxvuamozhilgrxtkndesy')
False
>>> validKey('jfbqpwvuamozhilgrxtkndesy')
False
>>> validKey('jfbqpwxvuamozhilgrxtkndesy')
False
>>> encode('bakery', 'jfbqpwcvuamozhilgrxtkndesy')
'fjmprs'
>>> encode('butchery', 'jfbqpwcvuamozhilgrxtkndesy')
'fktbvprs'
>>> encode('bullet', 'jfbqpwcvuamozhilgrxtkndesy')
'fkoopt'
>>> encode('bakery', 'jfbqpwxvuamozhilgrxtkndesy')
Traceback (most recent call last):
AssertionError: invalid key
>>> hasAlphabeticOrder('bakery')
False
>>> hasAlphabeticOrder('fjmprs')
True
>>> hasAlphabeticOrder('bullet')
False
>>> hasAlphabeticOrder('fkoopt')
True
>>> score('jfbqpwcvuamozhilgrxtkndesy', 'six-letter-words.txt')
60
>>> score('idsxvaqtobuefpgcjwzrkmhnyl', 'six-letter-words.txt')
474