forked from radyoP/scrabble_game
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trie.py
69 lines (62 loc) · 1.86 KB
/
trie.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import time
import string
import random
import sys
class Trie():
def __init__(self, words):
self._end = "_end_"
self.root = dict()
for word in words:
curr_dict = self.root
for letter in word:
curr_dict = curr_dict.setdefault(letter,{})
curr_dict[self._end] = self._end
def __contains__(self, item):
curr_dict = self.root
for letter in item:
if letter in curr_dict:
curr_dict = curr_dict[letter]
else:
return False
else:
if self._end in curr_dict:
return True
else:
return False
def __str__(self):
return str(self.root)
if __name__ == "__main__":
lowercase = string.ascii_lowercase
words = list()
for i in range(1000000):
word = ""
for i in range(random.randint(1,16)):
word += random.choice(lowercase)
words.append(word)
print("Created list of 1 000 000 random words")
#with open("wordsEn.txt", "r") as file:
# words = file.read().split()
t_i1 = time.time()
trie = Trie(words)
t_i2 = time.time()
t1 = time.time()
is_li = 0
is_tri = 0
for i in range(10000):
word = ""
for i in range(10):
word += random.choice(lowercase)
if word in words:
is_li += 1
words = []
t2 = time.time()
for i in range(10000):
word = ""
for i in range(10):
word += random.choice(lowercase)
if word in trie:
is_tri +=1
t3 = time.time()
is_ok = "OK" if is_li-is_tri else "NOT OK"
print(is_li)
print("Testing on 10k random words. List:",t2-t1,"s Trie", t3-t2, "s init time:", t_i2-t_i1, "s test was:", is_ok)