0% found this document useful (0 votes)
9 views2 pages

Test Solver

The document describes a trie data structure implementation in Python. It defines methods like add_word, add_wordlist, get_all, contains to add and search words. Small and big tests are included to test the trie functionality on sample and large word lists.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

Test Solver

The document describes a trie data structure implementation in Python. It defines methods like add_word, add_wordlist, get_all, contains to add and search words. Small and big tests are included to test the trie functionality on sample and large word lists.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

class trie:

def __init__(_):
_.d={};_.nof_words=0

def add_word(_,w):
cd=_.d
for l in w:
if l not in cd: cd[l]={};cd=cd[l]
else: cd=cd[l]
cd["endw"]=True;_.nof_words+=1

def add_wordlist(_,wl):
for w in wl:
_.add_word(w)

def get_all(_):
r=[]
def worker(cd,w):
for k in cd.keys():
if k=="endw":r.append(w)
else: worker(cd[k],w+k)
worker(_.d,"")
return r

def __has(_,w):
cd=_.d
for l in w:
if l in cd: cd=cd[l]
else: return False
return "endw" in cd

def __contains__(_,w):
return _.__has(w)

def walk(_,w,cd=None):
if cd==None: cd=_.d
done=False
for i in range(len(w)):
if w[i] in cd: cd=cd[w[i]]
else: break
if i==len(w)-1: done=True
return {
"here": cd,
"endw":"endw" in cd and done,
"preffix": None if done else w[:i],
"done": done
}

def __small_test():
# had to put this in a method, previously this code was run at module import
# move this code into a method somewhere, for archival
dct=trie()

lw=["what","where","warrior","war","in","","mark","marketing","marketerer"]
for w in lw:
dct.add_word(w)
print(dct.nof_words,len(lw))
dctall=dct.get_all()
print(*dctall)
print(dctall==lw)
sw=["war","wars","warrior","zebra","mar"]
for s in sw:
print(s,s in dct)

def __big_test():
from words_processing import get_all_lines
wl=get_all_lines("10kwordlist.txt",True)
dct=trie()
for w in wl:
dct.add_word(w)
print(dct.nof_words,len(wl))
cr=0
for w in wl[::-1]:
if w in dct: cr+=1
print(cr,len(wl))
dctall=dct.get_all()
print(wl==dctall)
print({*wl}=={*dctall})

You might also like