Advertisement
Guest User

Boggle coding challenge

a guest
Jan 26th, 2015
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.41 KB | None | 0 0
  1. class TreeNode(object):
  2.     def __init__(self, prefix, parent=None):
  3.         self.prefix = prefix
  4.         self.branches = {}
  5.         self.word = False
  6.         self.parent = parent
  7.  
  8.     def add(self, item):
  9.         if item == self.prefix: # done
  10.             self.word = True
  11.             return
  12.  
  13.         letter = item[len(self.prefix)]
  14.         if letter not in 'abcdefghijklmnopqrstuvwxyz':
  15.             return # not alphabet, nevermind
  16.  
  17.         if letter not in self.branches:
  18.             self.branches[letter] = TreeNode(item[0:len(self.prefix) + 1], self)
  19.         self.branches[letter].add(item)
  20.  
  21.     def dump(self, pad=''):
  22.         print "%s%s %s" % (pad, self.prefix, self.word,)
  23.         for letter in self.branches.iterkeys():
  24.             self.branches[letter].dump(pad + ' ')
  25.  
  26. class Dictionary(TreeNode):
  27.     def __init__(self):
  28.         super(Dictionary, self).__init__('')
  29.         print "loading dictionary ..."
  30.         for word in file('/usr/share/dict/american-english'):
  31.             word = word[0:-1].lower()
  32.             if len(word) > 1:
  33.                 self.add(word)
  34.         print "loaded."
  35.  
  36. def solvePuzzle(dictionary, puzzle):
  37.     results = []
  38.     visited = {}
  39.  
  40.     # recurse into puzzle, follow allong in tree-structure dictionary
  41.     def searchPuzzle(wordsofar, x, y, treeloc):
  42.         # off puzzle - return
  43.         if x >= len(puzzle[0]) or x < 0:
  44.             return
  45.         if y >= len(puzzle) or y < 0:
  46.             return
  47.  
  48.         # already used this letter - return
  49.         if "%s-%s" % (x, y) in visited:
  50.             return
  51.  
  52.         wordsofar = wordsofar + puzzle[y][x]
  53.         treeloc = treeloc.branches.get(wordsofar[-1])
  54.  
  55.         # no words in dictionary from this stem, give up
  56.         if not treeloc:
  57.             return
  58.  
  59.         # new word found
  60.         if treeloc.word and wordsofar not in results:
  61.             results.append(wordsofar)
  62.  
  63.         pos = "%s-%s" % (x, y)
  64.  
  65.         visited[pos] = True
  66.         searchPuzzle(wordsofar, x+1, y, treeloc)
  67.         searchPuzzle(wordsofar, x-1, y, treeloc)
  68.         searchPuzzle(wordsofar, x, y+1, treeloc)
  69.         searchPuzzle(wordsofar, x, y-1, treeloc)
  70.         del visited[pos]
  71.  
  72.     for x in range(0, len(puzzle[0])):
  73.         for y in range(0, len(puzzle)):
  74.             searchPuzzle('', x, y, dictionary)
  75.  
  76.     return results
  77.  
  78. if __name__ == '__main__':
  79.     print solvePuzzle(Dictionary(), ['xxdx', 'xxrt', 'xcax', 'xxxx'])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement