python - Caesar Cipher Recursion -


i attempting finish problem involving decoding string of text encoded multiple levels of caesar cipher. seems work first shift returning not recurse. have print statements throughout showing snippets using , putting functions , seem correct.

def build_decoder(shift):     cipher = build_coder(shift)     decoder = {}     k, v in cipher.items():         decoder[v] = k     return decoder  def is_word(wordlist, word):     word = word.lower()     word = word.strip(" !@#$%^&*()-_+={}[]|\:;'<>?,./\"")     return word in wordlist  def apply_coder(text, coder):     encrypted = []     character in text:         if character in coder.keys():             encrypted.append(coder[character])         else:             encrypted.append(character)     return ''.join(encrypted)  def apply_shift(text, shift):     coder = build_coder(shift)     return apply_coder(text, coder)  def apply_shifts(text, shifts):     index, shift in shifts:         text = (text[:index]) + (apply_coder(text[index:], build_coder(shift)))     return text  def find_best_shifts_rec(wordlist, text, start=0):             """             text: scrambled text try find words             start: start looking @ shifts             returns: list of tuples.  each tuple (position in text, amount of shift)             """             key = []             shift in range(28):                 message = text[:start] + apply_shift(text[start:], -shift) #concatenate text beginning start apply_shift start end of text.                 space = message[start:].find(" ") #find next space start " " character.                 if is_word(wordlist, message[start:space]): #if text start space word.                     print message[start:space]                     key.append((start, shift)) #add position , shift tuple in list key.                     print key                     print len(message[:start]), message[:start]                     print len(message[start:space]), message[start:space]                     print len(message), message                     print message[:space]                     print message[space+1:]                     print message[space+1]                     if not(is_word(wordlist, message[start:])):                         return message[:space] + find_best_shifts_rec(wordlist, message, space+1) #return text beginning space(decrypted) , recursively call find_best_shifts_rec on rest of text.                     else:                         return message[start:]             print "no shift match found, closest match:"             print key             return ''  s = apply_shifts("do androids dream of electric sheep?", [(0,6), (3, 18), (12, 16)]) print find_best_shifts_rec(wordlist, s) 

output:

do [(0, 6)] 0  2 36 sevif vjrkylhtgvmglslj ypjgzollw? sevif vjrkylhtgvmglslj ypjgzollw? s no shift match found, closest match: [] 

i assume mit 6.00 course? wrote working find_best_shifts , find_best_shifts_rec. first experience coding i'm sure code can improved, work, might able use baseline improve upon.

def find_best_shifts(wordlist, text): global shifts shifts = [] return find_best_shifts_rec(wordlist, text, 0)

def find_best_shifts_rec(wordlist, text, start):

for shift in range(28):         decoded = apply_shift(text[start:], shift)         words = decoded.split()         decoded = text[:start] + decoded         string_split = decoded.split()         size = len(string_split)         correct_words = 0         if is_word(wordlist, words[0]):             if shift != 0:                 shifts.append((start,shift))                 new_start = start + len(words[0]) + 1                 if new_start >= len(text)-1:                        return shifts                 else:                     return find_best_shifts_rec(wordlist, decoded, start=new_start)         j in string_split:             if is_word(wordlist, j):                 correct_words += 1                 if correct_words == size:                     return shifts 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -