-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path#11.py
80 lines (60 loc) · 1.48 KB
/
#11.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
70
71
72
73
74
75
76
77
78
79
80
class Node:
def __init__(self, c):
self.c = c
self.children = {}
def get_char(self):
return self.c
def contains(self, c):
return c in self.children
def get_node_for_char(self, c):
return self.children[c]
def add_child(self, n):
self.children[n.get_char] = n
def get_strings_with_prefix(self, prefix):
matches = []
for char, node in self.children:
if char == '*':
matches.append(prefix);
else:
matches = matches + node.get_strings_with_prefix(prefix + char)
return matches
class Trie:
def __init__(self, words):
self.roots = {}
for word in words:
curr = None
if word[0] in self.roots:
curr = self.roots[word[0]]
else:
curr = Node(word[0])
self.roots[word[0]] = curr
for c in word[1:]:
if curr.contains(c):
curr = curr.get_node_for_char(c)
else:
temp = Node(c)
curr.add_child(temp)
curr = temp
curr.add_child(Node('*'))
def query_prefix(self, prefix):
if prefix[0] in self.roots:
curr = self.roots[prefix[0]]
for c in prefix[1:]:
if curr.contains(c):
curr = curr.get_node_for_char(c)
else:
return None
return curr.get_strings_with_prefix(prefix)
else:
return None
words = input("Enter words: ").split(',')
dictionary = Trie(words)
while(True):
query = input("Enter query: ")
output = ""
result = dictionary.query_prefix(query)
if result == None:
print("No matches found.")
else:
for s in dictionary.query_prefix(query):
output = output + s + ", "