Contact book

Write the next functions and save them in file contact_book.py:

contact_book.make_contact_book(ldata)

Given a list of lists, ldata, where each sublist contains two strings that represent a name and a phone, return a dictionary whose keys are a string with the name, and the values a string with the phone number. Suppose there are no names repeated at ldata. For example:

>>> l = [["maria","931111111"], ["pep","912222222"], ["anna","93919391"]]
>>> d = make_contact_book(l)
>>> if d != {"maria":"931111111", "pep":"912222222", "anna":"93919391"}:
...    print(d)

Note

More tests are provided in file test-make_contact_book.txt.

contact_book.count_prefixs(contact_book, prefix)

Given a dictionary, contact_book, as the one returned by the function make_contact_book() and a telephone prefix (string), returns how many numbers of the contact book dictionary start with this prefix. For example,

>>> d = {"maria":"931111111", "pep":"912222222", "anna":"93119391"}
>>> count_prefix(d, '9311')
2
>>> count_prefix(d, '9')
3
>>> count_prefix(d, '67')
0

Note

More tests are provided in file test-count_prefix.txt.

contact_book.have_prefix(contact_book, prefix)

Given a dictionary, contact_book, as the one returned by the function make_contact_book() and a phone prefix (string), return a Boolean indicating if any phone of the contact book starts with the indicated prefix. For example,

>>> d = {"maria":"931111111", "pep":"912222222", "anna":"93119391"}
>>> have_prefix(d, '9311')
True
>>> have_prefix(d, '93117')
False
>>> have_prefix(d, '6')
False

Note

More tests are provided in file test-have_prefix.txt.

contact_book.names_prefix(contact_book, prefix)

Given a dictionary, contact_book, as the one returned by the function make_contact_book() and a telephone prefix (string), returns a list with the names that have a phone with this prefix, sorted alphabetically. For example,

>>> d = {"maria":"931111111", "pep":"912222222", "anna":"93119391"}
>>> names_prefix(d, '931')
['anna', 'maria']
>>> names_prefix(d, '9')
['anna', 'maria', 'pep']
>>> names_prefix(d, '6')
[]

Note

More tests are provided in file test-names_prefix.txt.

contact_book.make_contact_book_reps(ldata)

It does the same thing as function make_contact_book() but in this case in list ldata names may appear more than once. Returns a dictionary whose keys are the names, and each associated value is a list of strings with all telephone numbers that correspond to the name. The order of the phones of each name will be the same as the order they appear on the given list. For example,

>>> l = [["maria","931111111"], ["pep","912222222"], ["anna","93919391"],
... ["pep","66477333"], ["maria","665322888"]]
>>> d = make_contact_book_reps(l)
>>> if d != {"maria":["931111111","665322888"], "pep":["912222222","66477333"],
... "anna":["93919391"]}:
...    print(d)

Note

More tests are provided in file test-make_contact_book_reps.txt.

Solution

A solution of these functions is provided in file contact_book.py.