Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Dec 1st, 2012  |  syntax: Python  |  size: 1.18 KB  |  views: 73  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. def find_best_shifts(wordlist, text):
  2.     """
  3.    Given a scrambled string, returns a shift key that will decode the text to
  4.    words in wordlist, or None if there is no such key.
  5.  
  6.    Hint: Make use of the recursive function
  7.    find_best_shifts_rec(wordlist, text, start)
  8.  
  9.    wordlist: list of words
  10.    text: scambled text to try to find the words for
  11.    returns: list of tuples.  each tuple is (position in text, amount of shift)
  12.    
  13.    Examples:
  14.    >>> s = random_scrambled(wordlist, 3)
  15.    >>> s
  16.    'eqorqukvqtbmultiform wyy ion'
  17.    >>> shifts = find_best_shifts(wordlist, s)
  18.    >>> shifts
  19.    [(0, 25), (11, 2), (21, 5)]
  20.    >>> apply_shifts(s, shifts)
  21.    'compositor multiform accents'
  22.    >>> s = apply_shifts("Do Androids Dream of Electric Sheep?", [(0,6), (3, 18), (12, 16)])
  23.    >>> s
  24.    'JufYkaolfapxQdrnzmasmRyrpfdvpmEurrb?'
  25.    >>> shifts = find_best_shifts(wordlist, s)
  26.    >>> print apply_shifts(s, shifts)
  27.    Do Androids Dream of Electric Sheep?
  28.    """
  29.    
  30.     ""
  31.     s = find_best_shifts_rec(wordlist, text, 0)
  32.     shifts = []
  33.     s.reverse()
  34.     for c in s:
  35.         new = (c[0], -(c[1]))
  36.         shifts.append(new)
  37.     return shifts
clone this paste RAW Paste Data