本文整理汇总了Python中trie.Trie.iterate方法的典型用法代码示例。如果您正苦于以下问题:Python Trie.iterate方法的具体用法?Python Trie.iterate怎么用?Python Trie.iterate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类trie.Trie
的用法示例。
在下文中一共展示了Trie.iterate方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Board
# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import iterate [as 别名]
class Board(object):
def __init__(self, chars, dictionary, size=4):
self.trie = Trie()
for word in dictionary:
self.trie.insert(word)
self.tiles = [None] * size
for i in range(size):
self.tiles[i] = [None] * size
ixchar = 0
for y in range(size):
for x in range(size):
self.tiles[y][x] = Tile(chars[ixchar].lower())
ixchar += 1
for y in range(size):
for x in range(size):
for i, nix in enumerate(NEIGHBOR_IDX):
nx, ny = nix
if 0 <= nx + x < size and 0 <= ny + y < size:
self.tiles[y][x].neighbors[i] = self.tiles[ny + y][nx + x]
def solve(self):
words = set() # I was wrong last week: Python has sets but it didn't always (v2.3).
i = self.trie.iterate()
sideSize = len(self.tiles)
for iTile in range(sideSize * sideSize):
self.solveFromTile(i, self.tiles[iTile // sideSize][iTile % sideSize], words)
return words
def solveFromTile(self, i, tile, words):
tile.visited = True
if i.advance(tile.char):
if i.isWord():
words.add(i.value())
for n in tile.neighbors:
if n is not None and not n.visited:
self.solveFromTile(i, n, words)
i.pop()
tile.visited = False