Hey all,
So basically I need to change the code so that the phonebook and the inverse phonebook are easily updated and maintained. Also, whenever I add an entry, both phonebooks need to be updated.
Lastly, when I write a phonebook to a file, write a backwards phonebook as well.
Do your best to handle cases when two individuals share a phone number. If both Fido and Lucky have the same phone number (212-888-3434). One of them will not be in the backwards phonebook unless, the backwards phonebook is modified.
Suggestion: for the backwards phonebook, allow the values to be a list of names, rather than a single name. Alternatively, use a string that includes all of the barers of a phone number. Thus, in the above example, the value for '212-888-3434' could be 'Fido and Lucky' or .
Help would be GREATLY appreciated!
import os
telephone_book = {}
inverse_phonebook = {}
def add_number_to_phone_book(key,value):
telephone_book[key] = value
def add_line_to_phone_book(line):
words = line.split()
name = ''
if len(words) >= 2:
for word in words[:-1]:
name = name+' '+word
name = name[1:]
add_number_to_phone_book(name,words[-1])
else:
print('Warning null entry')
def add_phone_numbers_from_file(file):
new_numbers = open(file, 'r')
for line in new_numbers:
add_line_to_phone_book(line)
new_numbers.close()
def write_phone_numbers_to_file(file):
write = open(file,'w')
for key in telephone_book:
write.write(key+' '+ telephone_book[key]+os.linesep)
write.close()
def create_inverse_phone_book(dictionary):
inverse_phonebook.clear()
for key in dictionary:
inverse_phonebook[dictionary[key]] = key
def create_inverse_phone_book2(dictionary):
inverse_phonebook.clear()
for key in dictionary:
if telephone_book[key] in inverse_phonebook:
inverse_phonebook[dictionary[key]] = \
inverse_phonebook[dictionary[key]]\
+ ' and ' + key
else:
inverse_phonebook[dictionary[key]] = key
def create_inverse_phone_book3(dictionary):
## This is an alternative using lists as values
inverse_phonebook.clear()
for key in dictionary:
if telephone_book[key] in inverse_phonebook:
inverse_phonebook[dictionary[key]].append(key)
else:
inverse_phonebook[dictionary[key]] = [key]