当前位置: 首页>>代码示例>>Python>>正文


Python Trie.iterate方法代码示例

本文整理汇总了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
开发者ID:GitOnUp,项目名称:ADACollateral,代码行数:41,代码来源:solver.py


注:本文中的trie.Trie.iterate方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。