Skip to content

Instantly share code, notes, and snippets.

@anfedorov
Created April 28, 2010 23:56
Show Gist options
  • Save anfedorov/382921 to your computer and use it in GitHub Desktop.
Save anfedorov/382921 to your computer and use it in GitHub Desktop.
from collections import defaultdict
def new_trie():
t = defaultdict()
t.default_factory = lambda: [new_trie(), 0]
return t
def insert(t, s):
for c in s:
t = t[c][0]
t[c][1] += 1
return t[c][1]
def lookup(t, s):
for c in s:
t = t[c][0]
return t[c][1]
t = new_trie()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment