Answer:
Code in Python is given. Take note of the instruction in bold font
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\n')
for num,name in ph_num_dictionary.items():
if len(name)==1:
write_file.write(num+'\t'+name[0]+'\r\n')
else:
name.sort()
for person in name:
write_file.write(num + '\t' + person + '\r\n')
backward_phone_number('F:\\phone_number_input.txt','F:\\phone_number_output.txt')