175k views
3 votes
Make a Backwards Phonebook from a Regular Phonebook

Your function should take 2 parameters: infile and outfile

Infile should be a phonebook in the same format as question 2 (version 2).

Outfile should be a list of phone number people pairs, sorted by phone number

As with Question 2, you should account for the possibility that 2 people may share a single phone number (e.g., a land line if they live in the same home)

First read in all phone numbers into a dictionary, the same as you had done for Question 2. You can call functions defined in question 2 and use global variables from Question 2 as well if it makes sense to do so.

Read through that dictionary and create a backwards version.

In the new dictionary the keys are phone numbers and the values are lists of people

Use a similar strategy as with Version 2 of Question 2:

if a number is not in the dictionary, you create a new entry which is a list of a single person

Otherwise, you check the list associated with that number and you add a person's name if they are not already listed in the (backwards) phonebook

Create a list of keys from the dictionary and sort it.

For each key in the list, look up the corresponding entry/

For each name in the entry (also a list), write a line in the output firle consisting of a telephone number + a tab + a name

For example, given the sample file

Name Phone Number
Bob 212-888-0000
Bugs 917-433-3333
Daffy 914-400-4141
Daffy 800-999-0000
Daisy 914-400-4141
Daisy 123-456-7890
Pippi 301-999-9990
Pippi 800-333-4400
Yosemite 212-333-3333
as input, the program would produce

Telephone Number Name
123-456-7890 Daisy
212-333-3333 Yosemite
212-888-0000 Bob
301-999-9990 Pippi
800-333-4400 Pippi
800-999-0000 Daffy
914-400-4141 Daisy
914-400-4141 Daffy
917-433-3333 Bugs
as output (notice that Daffy and Daisy share a phone number).

User Msvalkon
by
7.9k points

1 Answer

3 votes

Answer:

Code in Python is given. Take note of the instruction in bold font

Step-by-step explanation:

You will need to update the below two lines in your system based on the file path -

backward_phone_number('F:\\phone_number_input.txt','F:\\phone_number_output.txt')

def backward_phone_number(input_file,output_file):

ph_num_dictionary={}

entries=[]

#########################################################

with open(input_file,'r') as read_file:

entries=read_file.readlines()[1:]

for entry in entries:

ph_num_name=entry.split()

ph_number=ph_num_name[1].strip()

name=ph_num_name[0].strip()

if ph_num_dictionary.get(ph_number)==None:

name_list=[name]

ph_num_dictionary[ph_number]=name_list

else:

ph_num_dictionary.get(ph_number).append(name)

#########################################################

ph_num_dictionary = dict(sorted(ph_num_dictionary.items(),key=lambda pair:pair[0]))

with open(output_file,'w+') as write_file:

write_file.write('Telephone Number\tName\r\\')

for num,name in ph_num_dictionary.items():

if len(name)==1:

write_file.write(num+'\t'+name[0]+'\r\\')

else:

name.sort()

for person in name:

write_file.write(num + '\t' + person + '\r\\')

backward_phone_number('F:\\phone_number_input.txt','F:\\phone_number_output.txt')

User Manuszep
by
8.1k points