dbwonders

ps5_ghost binary search

Apr 23rd, 2012
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.37 KB | None | 0 0
  1. import random
  2.  
  3. # -----------------------------------
  4. # Helper code
  5. # (you don't need to understand this helper code)
  6.  
  7. import string
  8.  
  9. WORDLIST_FILENAME = "words.txt"
  10.  
  11. def load_words():
  12.     """
  13.    Returns a list of valid words. Words are strings of lowercase letters.
  14.    Depending on the size of the word list, this function may
  15.    take a while to finish.
  16.    """
  17.     print "Loading word list from file..."
  18.     # inFile: file
  19.     inFile = open(WORDLIST_FILENAME, 'r', 0)
  20.     # wordlist: list of strings
  21.     wordlist = []
  22.     for line in inFile:
  23.         wordlist.append(line.strip().lower())
  24.     print "  ", len(wordlist), "words loaded."
  25.     return wordlist
  26.  
  27. def get_frequency_dict(sequence):
  28.     """
  29.    Returns a dictionary where the keys are elements of the sequence
  30.    and the values are integer counts, for the number of times that
  31.    an element is repeated in the sequence.
  32.    sequence: string or list
  33.    return: dictionary
  34.    """
  35.     # freqs: dictionary (element_type -> int)
  36.     freq = {}
  37.     for x in sequence:
  38.         freq[x] = freq.get(x,0) + 1
  39.     return freq
  40.  
  41. # (end of helper code)
  42. # -----------------------------------
  43.  
  44. # Actually load the dictionary of words and point to it with
  45. # the wordlist variable so that it can be accessed from anywhere
  46. # in the program.
  47.  
  48. wordlist = load_words()
  49.  
  50. # TO DO: your code begins here!
  51.  
  52. def ghost():
  53.     frag = ''
  54.     count = 0
  55.     while True:
  56.         print 'Current word fragment: ', frag
  57.         if count%2 == 0:
  58.             player = 'Player 1'
  59.         else: player = 'Player 2'
  60.  
  61.         print player,
  62.         lettera = raw_input('says: ')
  63.         letter = lettera.lower()
  64.         first = 0
  65.         last = len(wordlist)-1
  66.  
  67.         if letter in string.ascii_letters:
  68.             frag += letter
  69.             exists = exist_word(wordlist,frag,first,last)# set variable so it will run only once
  70.             if exists == False:
  71.                 print player, 'loses because no word begins with', frag.upper()
  72.                 break              
  73.             if exists == True and len(frag)>3:                        
  74.                     print player, 'loses because',frag.upper(), 'is a word!'
  75.                     break              
  76.             count += 1                    
  77.         else: print 'Invalid character. Try again.'
  78.     return frag
  79.  
  80. def fragment(frag, word):
  81.     if word.startswith(frag)==True:
  82.         return True
  83.     else: return False
  84.  
  85. def exist_word(wordlist, frag, first, last):
  86.     """
  87.    Binary search. If either last 2 words left is the frag, return True.
  88.    """  
  89.     #print first, last, call, wordlist[last], frag
  90.     mid = first + ((last-first)/2)
  91.     #print mid, wordlist[mid]
  92.  
  93.     ##End Test. Function runs until there are 2 words left
  94.     ##Note: The wordlist[last] is always less than word fragment or, if it exists,
  95.     ##1 before the fragment. Therefore, compare wordlist[last+1]
  96.  
  97.     if last-first<2:
  98.             if wordlist[first]==frag or wordlist[last]==frag:
  99.                 return True
  100.             elif fragment(frag, wordlist[last+1]) == False:
  101.                 return False
  102.             else:
  103.                 return None
  104.  
  105.     if wordlist[mid] == frag:
  106.             return True
  107.     if wordlist[mid] > frag:
  108.             return exist_word(wordlist, frag, first, mid-1)                
  109.     return exist_word(wordlist, frag, mid + 1, last)
  110.  
  111. ghost()
Advertisement
Add Comment
Please, Sign In to add comment