-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontacts.py
39 lines (35 loc) · 1 KB
/
contacts.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
class Node:
def __init__(self, count=1):
self.count = count
self.children = {}
class Contacts:
def __init__(self):
self.Nodes = {}
def add(self, name):
current = self.Nodes
for s in name:
if s in current:
current[s].count += 1
else:
current[s] = Node()
current = current[s].children
def findPartial(self, part):
current = self.Nodes
for s in part[:-1]:
if s in current:
current = current[s].children
else:
return 0
return current[part[-1]].count if part[-1] in current else 0
# read stdin
with open("contacts_input.txt") as f:
lines = f.readlines()
contacts = Contacts()
fp = open("contacts_output.txt", 'w')
for x in lines[1:]:
cmd, arg = (x.strip().split())
if cmd == 'add':
contacts.add(arg)
else:
fp.write(arg + ": " + str(contacts.findPartial(arg)) + '\n')
fp.close()