Beispiel #1
0
def _dfs_visit(
    cube: Tuple[int, int],
    grid: List[List[str]],
    word_list: pygtrie.Trie,
    word: str,
    cubes_visited: Set[Tuple[int, int]],
) -> Set[str]:
    """
    Visit the next state in a depth-first search for words in the letter grid.

    :param Tuple[int, int] cube: grid indices of a letter cube
    :param List[List[str]] grid: a grid of letter cubes to search
    :param pygtrie.Trie word_list: a Trie containing all valid words
    :param str word: a string of letters representing the current search path
    :param Set[Tuple[int, int]] cubes_visited: set of cube indices already visited
    :return: set of valid words found from the current search state
    :rtype: Set[str]
    """
    i, j = cube
    # Capitalize all words
    word += grid[i][j].upper()
    if not word_list.has_node(word):
        return set()
    words_found = {word} if word_list.has_key(word) and len(word) > 2 else set()
    neighbors = _get_neighboring_cubes(cube, grid, cubes_visited)
    neighboring_words = [
        _dfs_visit(n, grid, word_list, word, cubes_visited.union({cube}))
        for n in neighbors
    ]
    return reduce(lambda x, y: x.union(y), neighboring_words, words_found)
Beispiel #2
0
 def filter_legal_actions_for_vw_node(legal_actions: Tuple[int],
                                      is_recursive: bool,
                                      current_trie: pygtrie.Trie):
     if is_recursive:
         # We need to check that only pieces which can be extended are valid.
         legal_actions = [
             action for action in legal_actions
             if current_trie.has_subtrie((action, ))
         ]
     else:
         # We need to check that only pieces which yield a valid word are
         # valid.
         legal_actions = [
             action for action in legal_actions
             if current_trie.has_key((action, ))
         ]
     return legal_actions