-
Notifications
You must be signed in to change notification settings - Fork 0
/
phone_book.py
executable file
·63 lines (46 loc) · 1.75 KB
/
phone_book.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
# Author: jerrybelmonte
# Implement a simple phone book manager.
class PhoneBook:
def __init__(self):
self.contacts = dict()
def add(self, number: int, name: str):
"""Adds a contact to the phone book with the given name and number.
If the number already exists, the contact name will be overwritten."""
self.contacts[number] = name
def erase(self, number: int):
"""Erases a person with the given number from the phone book if and
and only if the contact number is in the phone book."""
if number in self.contacts:
del self.contacts[number]
def find(self, number: int):
"""Looks for a person with the given phone number. Returns a string
with the contact name, or 'not found' if the phone number is not in
the phone book."""
if number in self.contacts:
return self.contacts.get(number)
return 'not found'
class Query:
def __init__(self, query):
self.type = query[0]
self.number = int(query[1])
self.name = None
if self.type == 'add':
self.name = query[2]
def read_queries():
n = int(input())
return [Query(input().split()) for _ in range(n)]
def write_responses(result):
print('\n'.join(result))
def process_queries(queries):
result = []
phone_book = PhoneBook()
for cur_query in queries:
if cur_query.type == 'add':
phone_book.add(cur_query.number, cur_query.name)
elif cur_query.type == 'del':
phone_book.erase(cur_query.number)
elif cur_query.type == 'find':
result.append(phone_book.find(cur_query.number))
return result
if __name__ == '__main__':
write_responses(process_queries(read_queries()))