Created
May 2, 2023 16:21
-
-
Save hughdbrown/5899f54eef03a3234336209ca1e79989 to your computer and use it in GitHub Desktop.
A class for looking up words
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import defaultdict | |
def trie(): | |
return defaultdict(trie) | |
class Trie(object): | |
TERMINATOR = "<END>" | |
def __init__(self): | |
self.t = trie() | |
def insert_word(self, word): | |
t = self.t | |
for c in word: | |
t = t[c] | |
t[self.TERMINATOR] | |
def lookup_word(self, word): | |
t = self.t | |
for c in word: | |
if c in t: | |
t = t[c] | |
else: | |
return False | |
return self.TERMINATOR in t |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment