SE Computer Engineering Practical 18 solution
Question:
Write a python program to store names and mobile numbers of your friends in sorted order on names.
Search your friend from list using binary search (recursive and nonrecursive).
Insert friend if not present in phonebook
Code:
class Phonebook:
def __init__(self):
self.phonebook = []
def add_friend(self, name, mobile_number):
friend = (name, mobile_number)
self.phonebook.append(friend)
self.phonebook.sort(key=lambda x: x[0]) # Sort phonebook based on names
def binary_search_recursive(self, name, start, end):
if start > end:
return -1
mid = (start + end) // 2
if self.phonebook[mid][0] == name:
return mid
elif self.phonebook[mid][0] < name:
return self.binary_search_recursive(name, mid + 1, end)
else:
return self.binary_search_recursive(name, start, mid - 1)
def binary_search_iterative(self, name):
start = 0
end = len(self.phonebook) - 1
while start <= end:
mid = (start + end) // 2
if self.phonebook[mid][0] == name:
return mid
elif self.phonebook[mid][0] < name:
start = mid + 1
else:
end = mid - 1
return -1
def search_friend(self, name, method):
if method == "recursive":
index = self.binary_search_recursive(name, 0, len(self.phonebook) - 1)
elif method == "iterative":
index = self.binary_search_iterative(name)
else:
print("Invalid search method!")
return
if index != -1:
print("Friend found in the phonebook:")
print("Name:", self.phonebook[index][0])
print("Mobile Number:", self.phonebook[index][1])
else:
print("Friend not found in the phonebook.")
def display_phonebook(self):
print("Phonebook:")
for friend in self.phonebook:
print("Name:", friend[0])
print("Mobile Number:", friend[1])
print()
# Create a Phonebook object
phonebook = Phonebook()
# Get input from the user to add friends
print("Enter the details of your friends:")
while True:
name = input("Name (or 'exit' to stop): ")
if name.lower() == "exit":
break
mobile_number = input("Mobile Number: ")
phonebook.add_friend(name, mobile_number)
# Display the phonebook
phonebook.display_phonebook()
# Get input from the user to search for a friend
print("Search for a friend:")
friend_name = input("Enter the name of the friend: ")
search_method = input("Enter the search method (recursive/iterative): ")
phonebook.search_friend(friend_name, search_method)
# Get input from the user to add a friend if not present
print("Add a friend if not present:")
friend_name = input("Enter the name of the friend: ")
mobile_number = input("Enter the mobile number: ")
phonebook.add_friend(friend_name, mobile_number)
# Display the updated phonebook
phonebook.display_phonebook()
Output:
Enter the details of your friends: Name (or 'exit' to stop): Swarada Mobile Number: 6886129023 Name (or 'exit' to stop): Reena Mobile Number: 8766013345 Name (or 'exit' to stop): Rutuja Mobile Number: 4040606060 Name (or 'exit' to stop): Snehal Mobile Number: 5665658485 Name (or 'exit' to stop): exit Phonebook: Name: Reena Mobile Number: 8766013345 Name: Rutuja Mobile Number: 4040606060 Name: Snehal Mobile Number: 5665658485 Name: Swarada Mobile Number: 6886129023 Search for a friend: Enter the name of the friend: Reena Enter the search method (recursive/iterative): recursive Friend found in the phonebook: Name: Reena Mobile Number: 8766013345 Add a friend if not present: Enter the name of the friend: Shivani Enter the mobile number: 4444000045 Phonebook: Name: Reena Mobile Number: 8766013345 Name: Rutuja Mobile Number: 4040606060 Name: Shivani Mobile Number: 4444000045 Name: Snehal Mobile Number: 5665658485 Name: Swarada Mobile Number: 6886129023 Process finished with exit code 0