Python: Problem Set 4

Problem set 4 is to implement a Caesar Cipher including encryption and decrpytion of a text string. For example, encrypting a text by 1 and decrypting it:

>>> import ps4
Loading word list from file…
55909 words loaded.
>>> s = ps4.apply_shift(“The files are *in* the computer”, 1)
>>> s
>>> ps4.find_best_shift(ps4.wordlist, s)
>>> ps4.apply_coder(s, ps4.build_decoder(1))
The files are *in* the computer

On a multiple-shift decryption, my control flow only allows the function to decrypt text from left to right. That way we keep track of our position in the text as we shift our way across it, while also counting the number of shifts to find each valid word according to the dictionary. Until I implemented this, my valid_word checker might find a word farther down the string, move the ‘cursor’ and get completely derailed. This is obvious around line 475.

Another ‘ah-ha’ moment was trying to keep track of the counter through a recursive function. To do so, I turned the ‘start’ variable in the same function as above into an array instead of an int. start[0] functions as the cursor, start[1] as the number of shifts, and further tuples track where shifts started and how many occurred. I am not entirely sure this is allowed per the instructions, but the means are currently justifying the end result.