Last active
October 4, 2017 23:15
-
-
Save thatguyintech/4987417432af689f030517fff5bc14f6 to your computer and use it in GitHub Desktop.
This file contains 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
# add a word to an existing trie, recursively | |
def addWord(word, trie): | |
# non-base case | |
if word: | |
letter, rest = word[0], word[1:] | |
if letter not in trie: | |
trie[letter] = {} | |
addWord(rest, trie[letter]) | |
# base case | |
else: | |
trie['$'] = None | |
trie = {} | |
addWord("apple", trie) | |
addWord("app", trie) | |
addWord("appo", trie) | |
addWord("api", trie) | |
trie == {'a': {'p': {'i': {'$': None}, 'p': {'l': {'e': {'$': None}}, 'o': {'$': None}, '$': None}}}} # True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
lgtm!