Question

Write a telephone lookup PYTHON program. Read a data set of names and telephone numbers from...

Write a telephone lookup PYTHON program. Read a data set of names and telephone numbers from a file that contains the numbers in random order. Handle lookups by name and also reverse lookups by phone number. Use a binary search for both lookups. Use the following data set:

Bob|555-1234

Joe|555-2345

John|555-3456

Luke|555-4567

Mark|555-5678

Matthew|555-6789

The program should prompt the user as follows:

      L)ookup Name, Lookup N)umber or Q)uit?

      Enter the name:

         Or

      Enter the Number:

  1. Your code with comments
  2. A screenshot of the execution

Test Cases:

            Enter the name: John

            Phone number is 555-3456

            Enter the Number: 555-4567

            Name is Luke

0 0
Add a comment Improve this question Transcribed image text
Answer #1

`Hey,

Note: Brother in case of any queries, just comment in box I would be very happy to assist all your queries

Note: Brother to run this, data.txt should be in your directory as mentioned in question.

Note: Brother sometimes while uploading on HomeworkLib the indentations change. So, I request you to verify it with screenshot once. This is the link where I have saved the code too

https://onlinegdb.com/rJoFrBb3N

import sys

# opening the data file
f=open("data.txt")

#empty dictionary
d={}


#creating dictionary using name and phone numbers
for line in f:
line=line.strip()
data=line.split('|')
if data[0] not in d:
d[data[0].lower()]=data[1]


#searching for the phone by name
def binsearch_name(s):

# getting the name list
name=list(d.keys())

# sorting the list
name.sort()

# binary search
first = 0
last = len(name)-1
found = False
while( first<=last and not found):
mid = (first + last)//2
if name[mid].lower() == s :
found = True
else:
if s < name[mid].lower():
last = mid - 1
else:
first = mid + 1
# data found
if found:
return d[s]
else:
return "\nName not found"

#searching for the name by phone number
def binsearch_number(s):

# getting the phone number list
numbers=list(d.values())

# sorting the list
numbers.sort()

# implementing binary search
first = 0
last = len(numbers)-1
found = False
while( first<=last and not found):
mid = (first + last)//2
if numbers[mid] == s :
found = True
else:
if s < numbers[mid]:
last = mid - 1
else:
first = mid + 1

# data found
if found:
  
for k, v in d.items():
if v==s:
return k.capitalize()
else:
return "\nNumber not found"


# loop to get input
while True:

# getting user input
choice=input('\nL)ookup Name,Lookup N)umber or Q)uit? ')

choice=choice.upper()

if choice:

# getting number
if choice=='L':
number=input("\nEnter the number ")
print(binsearch_number(number))
#getting name
elif choice=='N':
name=input("\nEnter the name ")
name=name.lower()
print(binsearch_name(name))
elif choice =='Q':
sys.exit()
else:
print('\nEnter a valid choice')
continue

Kindly revert for any queries

Thanks.

Add a comment
Know the answer?
Add Answer to:
Write a telephone lookup PYTHON program. Read a data set of names and telephone numbers from...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Make the following modifications/enhancements to Version 0 of the Phonebook application: The phonebook should now contain...

    Make the following modifications/enhancements to Version 0 of the Phonebook application: The phonebook should now contain a first name as well as a last name. The format of the entries in the file should be: last-name first-name phone-number The lookup process now prompts for both a last and a first name A reverse lookup should also be provided, allowing a name to be obtained by supplying the phone number. Rather than continuing until the user signals end-of-file (at the keyboard),...

  • Write a Java program that creates and manipulates a directory of names, telephone numbers. The following...

    Write a Java program that creates and manipulates a directory of names, telephone numbers. The following information will be stored for each person in the directory: - Name (Last, First) - Home telephone number You should keep the entire collection ordered by key value (the combination of last and first names). Your program should be able to perform the following basic functions: - Search and display the contents of a particular entry - Display the entire directory - Delete an...

  • Write a simple telephone directory program in C++ that looks up phone numbers in a file...

    Write a simple telephone directory program in C++ that looks up phone numbers in a file containing a list of names and phone numbers. The user should be prompted to enter a first name and last name, and the program then outputs the corresponding number, or indicates that the name isn't in the directory. After each lookup, the program should ask the user whether they want to look up another number, and then either repeat the process or exit the...

  • In C++, Write a program that retrieves a phone number from a txt files of names...

    In C++, Write a program that retrieves a phone number from a txt files of names and phone numbers. The program should ask the user to enter a name and then print the phone number of that person (or vice versa, number into name) The program should allow the user to repeat the operation as often as they would like to. The data is contained in two text files named "phoneNames.txt" and "phoneNums.txt". The files have one name or one...

  • Write a program that has an array of at most 50 strings that hold people’s names...

    Write a program that has an array of at most 50 strings that hold people’s names and phone numbers. You can assume each string’s length is no more than 40. You may make up your own strings, or use the following: "Becky Warren, 555-1223" "Joe Looney, 555-0097" "Geri Palmer, 555-8787" "Lynn Presnell, 555-1212" "Holly Gaddis, 555-8878" "Sam Wiggins, 555-0998" "Bob Kain, 555-8712" "Tim Haynes, 555-7676" "Warren Gaddis, 555-9037" "Jean James, 555-4939" "Ron Palmer, 555-2783" The program should ask the user...

  • This is Python The program should accept input from the user as either 7-digit phone number...

    This is Python The program should accept input from the user as either 7-digit phone number or 10-digit. If the user enters 7 characters, the program should automatically add "512" to the beginning of the phone number as the default area code. Dash (hyphen) characters do not count in input character count, but must not be random. If the user enters dashes the total character count must not exceed 12. The program should not crash if the user enters invalid...

  • C++. You are to write a program to set up a data base for a phone...

    C++. You are to write a program to set up a data base for a phone index. The key will be a person’s name and the table will supply the phone number. The data base must be implemented as a hash table with the names as the key. The format will be a person name (multiple parts) followed by a phone number. Example:   John Doe   601-5433   or O E Vay 921-1234. Only one name one phone number will be on...

  • Write a program **(IN C)** that displays all the phone numbers in a file that match the area code...

    Write a program **(IN C)** that displays all the phone numbers in a file that match the area code that the user is searching for. The program prompts the user to enter the phone number and the name of a file. The program writes the matching phone numbers to the output file. For example, Enter the file name: phone_numbers.txt Enter the area code: 813 Output: encoded words are written to file: 813_phone_numbers.txt The program reads the content of the file...

  • write a code on .C file Problem Write a C program to implement a banking application...

    write a code on .C file Problem Write a C program to implement a banking application system. The program design must use a main and the below functions only. The program should use the below three text files that contain a set of lines. Sample data of these files are provided with the assessment. Note that you cannot use the library string.h to manipulate string variables. For the file operations and manipulations, you can use only the following functions: fopen(),...

  • Write an application that displays a series of at least five student ID numbers (that you...

    Write an application that displays a series of at least five student ID numbers (that you have stored in an array) and asks the user to enter a numeric test score for the student. Create a ScoreException class, and throw a ScoreException for the class if the user does not enter a valid score (less than or equal to 100). Catch the ScoreException, display the message Score over 100, and then store a 0 for the student’s score. At the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT