def test_prefix_trie_insert(corpus_words):
    trie = PrefixTrieNode()

    for word in corpus_words:
        trie.insert(word)

    assert len(trie) != 0
Exemplo n.º 2
0
def validate_board_answer(word, board_string, trie_root):
    trie_root = PrefixTrieNode(trie_root)

    board_matrix = board_string_to_matrix(board_string)
    N = len(board_matrix)
    M = len(board_matrix[0])

    queue = deque([])

    if len(word) == 0 or not trie_root.check_contain(word):
        return False

    for row in range(N):
        for col in range(M):
            if board_matrix[row][col] in [word[0], '*']:
                queue.append((row, col, 0, set()))

    pointer = 0
    while queue:
        for i in range(len(queue)):
            row, col, pointer, seen = queue.popleft()

            if pointer == len(word):
                return True

            elif not (0 <= row < N and 0 <= col < M) or (row, col) in seen:
                continue

            elif board_matrix[row][col] in ['*', word[pointer]]:
                for dx, dy in NEIGHBOURS:
                    queue.append(
                        (row + dy, col + dx, pointer + 1, seen | {(row, col)}))

    return False
Exemplo n.º 3
0
def build_prefix_trie(words):
    prefix_trie = PrefixTrieNode()
    for word in words:
        word = word.upper()
        prefix_trie.insert(word)

    return prefix_trie
def test_prefix_trie_not_contain(solution, wrong_words):
    trie = PrefixTrieNode()

    for word in solution:
        trie.insert(word)

    for wrong_word in wrong_words:
        assert trie.check_contain(wrong_word) == False, wrong_word
def test_prefix_autoset_key():
    trie = PrefixTrieNode()
    child_trie = trie['A']
    subchild_trie = child_trie['Z']

    assert isinstance(child_trie, PrefixTrieNode)
    assert isinstance(subchild_trie, PrefixTrieNode)

    assert len(trie) == 1
    assert len(child_trie) == 1
    assert len(subchild_trie) == 0
def test_prefix_trie_create():
    trie = PrefixTrieNode()
    assert len(trie) == 0
def test_prefix_trie_contain(corpus_words):
    trie = PrefixTrieNode()

    for word in corpus_words:
        trie.insert(word)
        assert trie.check_contain(word) == True