quizzy.py
#1. The input is a list of rings. Each ring is a list of letters. |
#2. Once you choose a letter from ring k then you can only choose letters from |
# ring k, k + 1, ... Outermost ring is ring 0. And ring depth increases inwards. |
#3. points[k] is the points for getting the letter chr(k + ord('A')) |
frombisectimportbisect, bisect_left |
fromtimeimporttime |
try: |
importpsyco |
psyco.full() |
exceptImportError: |
pass |
classSolver(object) : |
def__init__(self, ringfile, points= [1]*26, dictionary='/usr/share/dict/words') : |
''Setup the base. Use default scrabble scoring when points is unspecified. Use default Linux dictionary'' |
self.words= [i[:-1].upper() foriinopen(dictionary) ifi[:-1].isalpha()] |
self.words.sort() #just in case |
self.points=points |
self.parse(ringfile) |
default='endeoatifldiolrtnaolceteent' |
self.depth=len(self.rings) |
self.possibilities= [] |
self.tried=set() |
self.minlength, self.maxlength=3, 15 |
defparse(self, ringfile) : |
rings= [] |
forlineinopen(ringfile) : |
ifline.strip() : |
rings+= [list(line.strip().upper())] |
self.rings= [[[j, True] forjini] foriinrings] |
defisvalid(self, word) : |
''Check the validity of the word using binary search'' |
index=bisect(self.words, word) |
ifindex0orself.words[index-1] !=word : |
returnFalse |
returnTrue |
defwordworth(self, word) : |
''Return the points scored for 'word'' |
ret=0 |
forletterinword : |
ret+=self.points[ord(letter) -ord('A')] |
returnret |
defnowordwithstart(self, start) : |
''If there is no word that begins with start return True.'' |
index=bisect_left(self.words, start) |
try : |
ifself.words[index].startswith(start) : |
returnFalse |
exceptIndexError : |
returnTrue |
returnTrue |
defsuggest(self, tillnow=', curDepth=0) : |
''Recursive Depth First Search. Adds valid words to self.possibilities'' |
#not interested |
iflen(tillnow) >self.maxlengthorself.nowordwithstart(tillnow) ortillnowinself.tried: |
returnFalse |
#memoize : add to the list of tried ends |
self.tried.update([tillnow]) |
#if its a word then add to the list of possibilities |
iflen(tillnow) >=self.minlengthandself.isvalid(tillnow) : |
self.possibilities+= [(self.wordworth(tillnow), tillnow)] |
#pick more letters from this ring or a deeper ring |
foriinrange(curDepth, self.depth) : |
#in this depth pick a (letter, availability) pair |
forpairinself.rings[i] : |
#if the letter is availabile |
ifpair[1] : |
pair[1] =False |
#recurse : find words with this as a beginning |
self.suggest(tillnow+pair[0], i) |
pair[1] =True |
defsolve(self) : |
t=time() |
self.suggest() |
self.possibilities.sort(key=lambdax : x[0], reverse=True) |
forworth, wordinself.possibilities : printword, |
#print 'Took %.3f seconds' % (time() - t) |
if__name__'__main__': |
importos, sys |
iflen(sys.argv) >1andos.path.isfile(sys.argv[1]) : |
inst=Solver(sys.argv[1]) |
inst.solve() |
else : |
print'Did you give the right filename?' |
# example input file |
# |
# endeoatifldiolrt |
# aolcetee |
# t |
Quizzy's Word Challenge! Is a Deluxe only game in the arcade. You make as many words as you can. Longer words give you more points, and shorter words give you less points. Quizzy's Word Challenge! Basic Info Location: Quizzy's Word Challenge!

Word Challenge Game
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment