-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbaby_names.py
75 lines (58 loc) · 1.83 KB
/
baby_names.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
class Node(object):
def __init__(self, letter, end=False):
self.letter = letter
self.end = end
self.children = {}
def add_child(self, node):
self.children[node.letter] = node
def has_child(self, letter):
return letter in self.children
def get_child(self, letter):
if self.has_child(letter):
return self.children[letter]
else:
raise ValueError
def __repr__(self):
return "Letter: %s, End: %s, Children %s" % (
self.letter, self.end, self.children.keys())
class Trie(object):
def __init__(self):
self.root = Node('', True)
def add_word(self, word):
ptr = self.root
for letter in word:
if ptr.has_child(letter):
ptr = ptr.get_child(letter)
else:
child = Node(letter)
ptr.add_child(child)
ptr = child
# mark this node to have a complete word
ptr.end = True
def exists(self, word):
ptr = self.root
for letter in word:
if ptr.has_child(letter):
ptr = ptr.get_child(letter)
else:
return False
return ptr.end
def __str__(self):
ptr = self.root
return "%s" % ptr.children
baby_names = ['gaga', 'goo', 'gag']
garbled_text = 'gagagoogoogag'
def find_baby_names(baby_names, garbled_text):
trie = Trie()
for word in baby_names:
trie.add_word(word)
found_words = []
start, end = 0, len(garbled_text)
while end > start:
if trie.exists(garbled_text[start:end]):
found_words.append(garbled_text[start:end])
start, end = end, len(garbled_text)
else:
end -= 1
print ' '.join(found_words)
find_baby_names(baby_names, garbled_text)