def test_false(self): x = 'A' trie = graph.Graph() trie.node('A') trie.node('B') trie.edge('f', 'A', 'B', 'c') val = trie_methods.arrived(x, trie) self.assertFalse(val)
def test_normal(self): x = 'B' trie = graph.Graph() trie.node('A') trie.node('B') trie.edge('f', 'A', 'B', 'c') val = trie_methods.arrived(x, trie) self.assertTrue(val)
def is_prefix(text, trie): # is_prefix determines whether a given text, starting from the text's # first character, matches a branch of # the given trie, and is thus one of the patterns from which # the trie was constructed. if not (isinstance(text, str) or isinstance(trie, graph.Graph)): raise TypeError('ERROR: is_prefix requires string and trie.') else: # set the current node to the root of the trie. x = trie_methods.root(trie) # add an extra space at the end of the text to ensure the algorithm # runs to completion and identifies when it has arrived at a leaf. text = text + ' ' # iterate through the symbols in the text. for s in text: # if the current node has arrived at a leaf, # then a pattern matches this text. if trie_methods.arrived(x, trie): return True # if the current node has not arrived at a leaf, # but an edge exists matching # the current symbol, select that edge and travel to # that edge's codomain. elif trie_methods.edge_exists(s, x, trie): e = trie_methods.select(s, x, trie) x = trie.cod[e] else: # if no such edge exists, the current text # does not match any pattern. return False