From 0c0755cd6087679207063d2d31fed9a2cda23f31 Mon Sep 17 00:00:00 2001 From: Haidong Ji Date: Sat, 9 Mar 2019 19:44:21 -0600 Subject: Phone book done! --- sources/phone_book.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 sources/phone_book.py diff --git a/sources/phone_book.py b/sources/phone_book.py new file mode 100644 index 0000000..7b2b20a --- /dev/null +++ b/sources/phone_book.py @@ -0,0 +1,36 @@ +# python3 + + +class Query: + def __init__(self, query): + self.type = query[0] + self.number = int(query[1]) + if self.type == 'add': + self.name = query[2] + + +def read_queries(): + n = int(input()) + return [Query(input().split()) for i in range(n)] + + +def write_responses(result): + print('\n'.join(result)) + + +def process_queries(queries): + result = [] + # Keep list of all existing (i.e. not deleted yet) contacts. + contacts = ['not found'] * 10000000 + for cur_query in queries: + if cur_query.type == 'add': + contacts[cur_query.number] = cur_query.name + elif cur_query.type == 'del': + contacts[cur_query.number] = 'not found' + else: + result.append(contacts[cur_query.number]) + return result + + +if __name__ == '__main__': + write_responses(process_queries(read_queries())) -- cgit v1.2.3