Skip to content

Instantly share code, notes, and snippets.

@hughdbrown
Created May 2, 2023 16:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hughdbrown/5899f54eef03a3234336209ca1e79989 to your computer and use it in GitHub Desktop.
Save hughdbrown/5899f54eef03a3234336209ca1e79989 to your computer and use it in GitHub Desktop.
A class for looking up words
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