Guest User

PS4 problem 4 solution

a guest
Apr 10th, 2014
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.95 KB | None | 0 0
  1. def find_best_shifts(wordlist, text):
  2.    
  3.     return find_best_shifts_rec(wordlist, text, 0)
  4.  
  5.  
  6. def find_spaces(text, start):
  7.     """
  8.    given a text and start position, find all the spaces
  9.    returns a list of all the indices. empty list if none
  10.    """
  11.     tally = []
  12.     tmptext = text[start:]
  13.     counter = 0
  14.     for i in tmptext:
  15.         if i == " ":
  16.             tally.append(counter+start)
  17.         counter+=1
  18.     return tally
  19.  
  20.  
  21. def is_tupleorlist(arg1):
  22.     """
  23.    checks if something is a list or a tuple
  24.    """
  25.     if type(arg1) == tuple or type(arg1) == list:
  26.         return True
  27.     else:
  28.         return False
  29.  
  30. def find_best_shifts_rec(wordlist, text, start):
  31.     """
  32.    Given a scrambled string and a starting position from which
  33.    to decode, returns a shift key that will decode the text to
  34.    words in wordlist, or None if there is no such key.
  35.    wordlist: list of words
  36.    text: scambled text to try to find the words for
  37.    start: where to start looking at shifts
  38.    returns: list of tuples.  each tuple is (position in text, amount of shift)
  39.    """
  40.  
  41.     """for every possible shift from 0 to 27"""
  42.     for n in range (0,27):
  43.  
  44.         answer = []
  45.         curr_shift_tuple = start,-n
  46.  
  47.         """set s to be the text up to the start parameter concatenated with the text
  48.        after the start parameter that has been shifted by n"""
  49.         s = text[:start] + apply_coder(text[start:], build_decoder(n))
  50.  
  51.         """look for the spaces beginning at the start location"""
  52.         spaceslist = find_spaces(s, start)
  53.  
  54.         """if we find a space and the point from start to first space are a valid word then we
  55.        recurse the function starting at the point of the space
  56.        There's only 2 possible outcomes from doing our shift. Either there's a space in the shifted
  57.        chunk or there isn't, which means we should test of that chunk is a word or not (we're at the
  58.        end of the sentence possibly)
  59.        """
  60.         if spaceslist != []:
  61.             j = s[start:spaceslist[0]] #j is the first word from start
  62.             if is_word(wordlist, j): #check if j is a word
  63.                 foo = find_best_shifts_rec(wordlist, s, spaceslist[0]+1)
  64.                 if is_tupleorlist(foo): #if the recursive call returns a tuple then we've found an answer
  65.                     answer.append(curr_shift_tuple)
  66.                     answer += foo
  67.                     return answer
  68.                     #if foo is not a tuple then do nothing and continue checking range
  69.         elif spaceslist == []:
  70.             j = s[start:len(s)] #j is the string from start to the end of s
  71.             if is_word(wordlist, j): #check if j is a word
  72.                 answer.append(curr_shift_tuple)
  73.                 return answer
  74.  
  75.     return None
  76.  
  77.  
  78. foo2 = "dnbxivmcmzuwzmhuebgsfdhsnyhepvwumpikzlydi p aivl hfgesffsfnglqqc eyryofdeiziqjz xk"
  79. bar = find_best_shifts(wordlist, foo2)
  80. print bar
  81. print apply_shifts(foo2, bar)
Advertisement
Add Comment
Please, Sign In to add comment