Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- # -----------------------------------
- # Helper code
- # (you don't need to understand this helper code)
- import string
- WORDLIST_FILENAME = "words.txt"
- def load_words():
- """
- Returns a list of valid words. Words are strings of lowercase letters.
- Depending on the size of the word list, this function may
- take a while to finish.
- """
- print "Loading word list from file..."
- # inFile: file
- inFile = open(WORDLIST_FILENAME, 'r', 0)
- # wordlist: list of strings
- wordlist = []
- for line in inFile:
- wordlist.append(line.strip().lower())
- print " ", len(wordlist), "words loaded."
- return wordlist
- def get_frequency_dict(sequence):
- """
- Returns a dictionary where the keys are elements of the sequence
- and the values are integer counts, for the number of times that
- an element is repeated in the sequence.
- sequence: string or list
- return: dictionary
- """
- # freqs: dictionary (element_type -> int)
- freq = {}
- for x in sequence:
- freq[x] = freq.get(x,0) + 1
- return freq
- # (end of helper code)
- # -----------------------------------
- # Actually load the dictionary of words and point to it with
- # the wordlist variable so that it can be accessed from anywhere
- # in the program.
- wordlist = load_words()
- # TO DO: your code begins here!
- def ghost():
- frag = ''
- count = 0
- while True:
- print 'Current word fragment: ', frag
- if count%2 == 0:
- player = 'Player 1'
- else: player = 'Player 2'
- print player,
- lettera = raw_input('says: ')
- letter = lettera.lower()
- first = 0
- last = len(wordlist)-1
- if letter in string.ascii_letters:
- frag += letter
- exists = exist_word(wordlist,frag,first,last)# set variable so it will run only once
- if exists == False:
- print player, 'loses because no word begins with', frag.upper()
- break
- if exists == True and len(frag)>3:
- print player, 'loses because',frag.upper(), 'is a word!'
- break
- count += 1
- else: print 'Invalid character. Try again.'
- return frag
- def fragment(frag, word):
- if word.startswith(frag)==True:
- return True
- else: return False
- def exist_word(wordlist, frag, first, last):
- """
- Binary search. If either last 2 words left is the frag, return True.
- """
- #print first, last, call, wordlist[last], frag
- mid = first + ((last-first)/2)
- #print mid, wordlist[mid]
- ##End Test. Function runs until there are 2 words left
- ##Note: The wordlist[last] is always less than word fragment or, if it exists,
- ##1 before the fragment. Therefore, compare wordlist[last+1]
- if last-first<2:
- if wordlist[first]==frag or wordlist[last]==frag:
- return True
- elif fragment(frag, wordlist[last+1]) == False:
- return False
- else:
- return None
- if wordlist[mid] == frag:
- return True
- if wordlist[mid] > frag:
- return exist_word(wordlist, frag, first, mid-1)
- return exist_word(wordlist, frag, mid + 1, last)
- ghost()
Advertisement
Add Comment
Please, Sign In to add comment