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
‘Uifagjmftabsfa*jo*auifadpnqvufs’
>>> ps4.find_best_shift(ps4.wordlist, s)
1
>>> 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 |
# 6.00 Problem Set 4 # # Caesar Cipher Skeleton # import string import random WORDLIST_FILENAME = "words.txt" # ----------------------------------- # Helper code # (you don't need to understand this helper code) def load_words(): """ Returns a list of valid words. Words are strings of lowercase letters. Depending on the size of the word list, this function may take a while to finish. """ print "Loading word list from file..." # inFile: file inFile = open(WORDLIST_FILENAME, 'r', 0) # line: string line = inFile.readline() # wordlist: list of strings wordlist = line.split() print " ", len(wordlist), "words loaded." return wordlist wordlist = load_words() def is_word(wordlist, word): """ Determines if word is a valid word. wordlist: list of words in the dictionary. word: a possible word. returns True if word is in wordlist. Example: >>> is_word(wordlist, 'bat') returns True >>> is_word(wordlist, 'asdf') returns False """ word = word.lower() word = word.strip(" !@#$%^&*()-_+={}[]|\:;'<>?,./\"") return word in wordlist def random_word(wordlist): """ Returns a random word. wordlist: list of words returns: a word from wordlist at random """ return random.choice(wordlist) def random_string(wordlist, n): """ Returns a string containing n random words from wordlist wordlist: list of words returns: a string of random words separated by spaces. """ return " ".join([random_word(wordlist) for _ in range(n)]) def random_scrambled(wordlist, n): """ Generates a test string by generating an n-word random string and encrypting it with a sequence of random shifts. wordlist: list of words n: number of random words to generate and scamble returns: a scrambled string of n random words NOTE: This function will ONLY work once you have completed your implementation of apply_shifts! """ s = random_string(wordlist, n) + " " shifts = [(i, random.randint(0, 26)) for i in range(len(s)) if s[i-1] == ' '] return apply_shifts(s, shifts)[:-1] def get_fable_string(): """ Returns a fable in encrypted text. """ f = open("fable.txt", "r") fable = str(f.read()) f.close() return fable # (end of helper code) # ----------------------------------- # # Problem 1: Encryption # def build_coder(shift): """ Returns a dict that can apply a Caesar cipher to a letter. The cipher is defined by the shift value. Ignores non-letter characters like punctuation and numbers. shift: -27 < int < 27 returns: dict Example: >>> build_coder(3) {' ': 'c', 'A': 'D', 'C': 'F', 'B': 'E', 'E': 'H', 'D': 'G', 'G': 'J', 'F': 'I', 'I': 'L', 'H': 'K', 'K': 'N', 'J': 'M', 'M': 'P', 'L': 'O', 'O': 'R', 'N': 'Q', 'Q': 'T', 'P': 'S', 'S': 'V', 'R': 'U', 'U': 'X', 'T': 'W', 'W': 'Z', 'V': 'Y', 'Y': 'A', 'X': ' ', 'Z': 'B', 'a': 'd', 'c': 'f', 'b': 'e', 'e': 'h', 'd': 'g', 'g': 'j', 'f': 'i', 'i': 'l', 'h': 'k', 'k': 'n', 'j': 'm', 'm': 'p', 'l': 'o', 'o': 'r', 'n': 'q', 'q': 't', 'p': 's', 's': 'v', 'r': 'u', 'u': 'x', 't': 'w', 'w': 'z', 'v': 'y', 'y': 'a', 'x': ' ', 'z': 'b'} (The order of the key-value pairs may be different.) """ ### TODO. assert(-27 < shift < 27) alphabet = [' '] i = 97 # build array of alphabet chars beginning with ' ' while i < 123: alphabet.append(chr(i)) i += 1 # create dict where key is letter and value is letter + shift coder = {} i = 0 while i < len(alphabet): coder[alphabet[i]] = alphabet[(i + shift) % len(alphabet)] i += 1 i = 1 # repeat for uppercase while i < len(alphabet): coder[alphabet[i].upper()] = alphabet[(i + shift) % len(alphabet)].upper() i += 1 return coder def build_encoder(shift): """ Returns a dict that can be used to encode a plain text. For example, you could encrypt the plain text by calling the following commands >>>encoder = build_encoder(shift) >>>encrypted_text = apply_coder(plain_text, encoder) The cipher is defined by the shift value. Ignores non-letter characters like punctuation and numbers. shift: 0 <= int < 27 returns: dict Example: >>> build_encoder(3) {' ': 'c', 'A': 'D', 'C': 'F', 'B': 'E', 'E': 'H', 'D': 'G', 'G': 'J', 'F': 'I', 'I': 'L', 'H': 'K', 'K': 'N', 'J': 'M', 'M': 'P', 'L': 'O', 'O': 'R', 'N': 'Q', 'Q': 'T', 'P': 'S', 'S': 'V', 'R': 'U', 'U': 'X', 'T': 'W', 'W': 'Z', 'V': 'Y', 'Y': 'A', 'X': ' ', 'Z': 'B', 'a': 'd', 'c': 'f', 'b': 'e', 'e': 'h', 'd': 'g', 'g': 'j', 'f': 'i', 'i': 'l', 'h': 'k', 'k': 'n', 'j': 'm', 'm': 'p', 'l': 'o', 'o': 'r', 'n': 'q', 'q': 't', 'p': 's', 's': 'v', 'r': 'u', 'u': 'x', 't': 'w', 'w': 'z', 'v': 'y', 'y': 'a', 'x': ' ', 'z': 'b'} (The order of the key-value pairs may be different.) HINT : Use build_coder. """ ### TODO. assert( 0 <= shift < 27) encoder = build_coder(shift) return encoder # watch it run # print build_encoder(3) def build_decoder(shift): """ Returns a dict that can be used to decode an encrypted text. For example, you could decrypt an encrypted text by calling the following commands >>>encoder = build_encoder(shift) >>>encrypted_text = apply_coder(plain_text, encoder) >>>decrypted_text = apply_coder(plain_text, decoder) The cipher is defined by the shift value. Ignores non-letter characters like punctuation and numbers. shift: 0 <= int < 27 returns: dict Example: >>> build_decoder(3) {' ': 'x', 'A': 'Y', 'C': ' ', 'B': 'Z', 'E': 'B', 'D': 'A', 'G': 'D', 'F': 'C', 'I': 'F', 'H': 'E', 'K': 'H', 'J': 'G', 'M': 'J', 'L': 'I', 'O': 'L', 'N': 'K', 'Q': 'N', 'P': 'M', 'S': 'P', 'R': 'O', 'U': 'R', 'T': 'Q', 'W': 'T', 'V': 'S', 'Y': 'V', 'X': 'U', 'Z': 'W', 'a': 'y', 'c': ' ', 'b': 'z', 'e': 'b', 'd': 'a', 'g': 'd', 'f': 'c', 'i': 'f', 'h': 'e', 'k': 'h', 'j': 'g', 'm': 'j', 'l': 'i', 'o': 'l', 'n': 'k', 'q': 'n', 'p': 'm', 's': 'p', 'r': 'o', 'u': 'r', 't': 'q', 'w': 't', 'v': 's', 'y': 'v', 'x': 'u', 'z': 'w'} (The order of the key-value pairs may be different.) HINT : Use build_coder. """ ### TODO. ### bug KeyError on decoder['C'] ### something to do with the do uppercase and i starting at 1? decoder = {} # get a copy of the encoding dict temp = build_coder(shift) # reverse values -> keys for i in temp: decoder[temp[i]] = i return decoder # watch it run # print build_decoder(3) def apply_coder(text, coder): """ Applies the coder to the text. Returns the encoded text. text: string coder: dict with mappings of characters to shifted characters returns: text after mapping coder chars to original text Example: >>> apply_coder("Hello, world!", build_encoder(3)) 'Khoor,czruog!' >>> apply_coder("Khoor,czruog!", build_decoder(3)) 'Hello, world!' """ ### TODO. ### 4/19/15 ### currently KeyError on punctuation encoded_char = [] for i in text: # ignore any letter/symbols not present in the coder dict (punctuation) if i in coder: encoded_char.append(coder[i]) else: encoded_char.append(i) encoded_text = ''.join(encoded_char) return encoded_text # watch it run # print apply_coder("Hello, world!", build_encoder(3)) def apply_shift(text, shift): """ Given a text, returns a new text Caesar shifted by the given shift offset. The empty space counts as the 27th letter of the alphabet, so spaces should be replaced by a lowercase letter as appropriate. Otherwise, lower case letters should remain lower case, upper case letters should remain upper case, and all other punctuation should stay as it is. text: string to apply the shift to shift: amount to shift the text returns: text after being shifted by specified amount. Example: >>> apply_shift('This is a test.', 8) 'Apq hq hiham a.' """ ### TODO. encoded_text = apply_coder(text, build_encoder(shift)) return encoded_text # watch it run # print apply_shift('This is a test.', 8) # # Problem 2: Codebreaking. # def find_best_shift(wordlist, text): """ Decrypts the encoded text and returns the plaintext. text: string returns: 0 <= int 27 Example: >>> s = apply_coder('Hello, world!', build_encoder(8)) >>> s 'Pmttw,hdwztl!' >>> find_best_shift(wordlist, s) returns 8 >>> apply_coder(s, build_decoder(8)) returns 'Hello, world!' """ ### TODO ### returns the shift int answer split_string = list(text) alpha_string = [] for i in split_string: if i.isalpha(): alpha_string.append(i) else: continue # print alpha_string i = 0 while i < 27: potential_decode = apply_shift(''.join(alpha_string), i) # print potential_decode # split result potential_decode = potential_decode.split() # all words in dictionary? winner = True for word in potential_decode: if word.lower() in wordlist: continue else: winner = False if winner == True: return abs(i-27) # all good things must come to an end i += 1 return None # watch it run # s = apply_coder('Hello, world!', build_encoder(8)) # print find_best_shift(wordlist, s) # print apply_coder(s, build_decoder(8)) # # Problem 3: Multi-level encryption. # def apply_shifts(text, shifts): """ Applies a sequence of shifts to an input text. text: A string to apply the Ceasar shifts to shifts: A list of tuples containing the location each shift should begin and the shift offset. Each tuple is of the form (location, shift) The shifts are layered: each one is applied from its starting position all the way through the end of the string. returns: text after applying the shifts to the appropriate positions Example: >>> apply_shifts("Do Androids Dream of Electric Sheep?", [(0,6), (3, 18), (12, 16)]) 'JufYkaolfapxQdrnzmasmRyrpfdvpmEurrb?' """ ### TODO. # for each pair in shifts (text, shifts) for i in shifts: # get text to shift from [text:] text_to_shift = text[i[0]:] # get prefix to conserve from [:text] text = text[:i[0]] # shift the text by shifts shifted_text = apply_shift(text_to_shift, i[1]) # recombine text = text + shifted_text return text # watch it run # print apply_shifts("Do Androids Dream of Electric Sheep?", [(0,6), (3, 18), (12, 16)]) # # Problem 4: Multi-level decryption. # def find_best_shifts(wordlist, text): """ Given a scrambled string, returns a shift key that will decode the text to words in wordlist, or None if there is no such key. Hint: Make use of the recursive function find_best_shifts_rec(wordlist, text, start) wordlist: list of words text: scambled text to try to find the words for returns: list of tuples. each tuple is (position in text, amount of shift) Examples: >>> s = random_scrambled(wordlist, 3) >>> s 'eqorqukvqtbmultiform wyy ion' >>> shifts = find_best_shifts(wordlist, s) >>> shifts [(0, 25), (11, 2), (21, 5)] >>> apply_shifts(s, shifts) 'compositor multiform accents' >>> s = apply_shifts("Do Androids Dream of Electric Sheep?", [(0,6), (3, 18), (12, 16)]) >>> s 'JufYkaolfapxQdrnzmasmRyrpfdvpmEurrb?' >>> shifts = find_best_shifts(wordlist, s) >>> print apply_shifts(s, shifts) Do Androids Dream of Electric Sheep? """ def find_best_shifts_rec(wordlist, text, start): """ Given a scrambled string and a starting position from which to decode, returns a shift key that will decode the text to words in wordlist, or None if there is no such key. Hint: You will find this function much easier to implement if you use recursion. wordlist: list of words text: scambled text to try to find the words for start: where to start looking at shifts returns: list of tuples. each tuple is (position in text, amount of shift) """ ### TODO. ########## # my logic assumes that words will be decrypted from left to right in order # This seems inefficient # I think I am abusing the start variable to pass counting values ########## # best case scenario, start at end AKA no shifts, check if words in dict # track the number of shifts in position start[1] if len(start) < 2: start.append(0) print text # is every item in result in dictionary? text_given = text.split() done_shifting = True # best case scenario for i in text_given: if is_word(wordlist, i): continue else: done_shifting = False # best case didn't happen if done_shifting == True: print "decipher complete" print text return start[2:] # save any valid words before start valid_words = text[:start[0]] # shift text after start and count it text_to_shift = text[start[0]:] shifted_text = apply_shift(text_to_shift, 1) start[1] += 1 # split on ' ' and check results for valid words split_shifted_text = shifted_text.split(' ') # recombine into a string to later resend to recursive function unsplit_shifted_text = ' '.join(split_shifted_text) recombined_text = valid_words + unsplit_shifted_text # check for valid words and move start for each start_began = start[0] for i in split_shifted_text: if is_word(wordlist, i): # watch it find words print i, " is a word" a = raw_input(">") start[0] += len(i)+1 else: break # assures decrypt from left to right if start[0] != start_began: start.append((start_began, abs(start[1] - 27))) start[1] = 0 # reset shifting counter # will always run one extra cycle to check correctness at the top return find_best_shifts_rec(wordlist, recombined_text, start) # watch it run # s = random_scrambled(wordlist, 3) # or # s = apply_shifts("Do Androids Dream of Electric Sheep?", [(0,25), (3, 1), (12, 1)]) # print find_best_shifts_rec(wordlist, s, [0]) def decrypt_fable(): """ Using the methods you created in this problem set, decrypt the fable given by the function get_fable_string(). Once you decrypt the message, be sure to include as a comment at the end of this problem set how the fable relates to your education at MIT. returns: string - fable in plain text """ ### TODO. s = get_fable_string() print find_best_shifts_rec(wordlist, s, [0]) # fails on 30th word # goes into infinite loop # potentially a problem with a double-space and my recombined strings # decrypt_fable() |