dbwonders

ghost

Apr 28th, 2012
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def fragment(frag, word):
  2.     if word.startswith(frag)==True:
  3.         return True
  4.     else: return False
  5.  
  6. def exist_word(wordlist, frag, first, last):
  7.     """
  8.    Binary search.
  9.    """
  10.    
  11.     print first, last, call, wordlist[last], frag
  12.     mid = first + ((last-first)/2)
  13.     print mid, wordlist[mid]
  14.    
  15.     ##End Test. Function runs until there are 2 words left
  16.     ##Note: The wordlist[last] is always less than word fragment or, if it exists,
  17.     ## 1 before the fragment. Therefore, compare with wordlist[last+1]  
  18.     if last-first<2:
  19.             if wordlist[first]==frag or wordlist[last]==frag:
  20.                 return True
  21.             elif fragment(frag, wordlist[last+1]) == False:
  22.                 return False
  23.             else:
  24.                 return None
  25.  
  26.    
  27.     if wordlist[mid] == frag:
  28.             return True
  29.     if wordlist[mid] > frag:
  30.             return exist_word(wordlist, frag, first, mid-1)                
  31.     return exist_word(wordlist, frag, mid + 1, last)
Advertisement
Add Comment
Please, Sign In to add comment