SE Computer Engineering Practical 19 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 Fibonacci search.
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 fibonacci_search(self, name):
fib_minus_2 = 0
fib_minus_1 = 1
fib = fib_minus_1 + fib_minus_2
while fib < len(self.phonebook):
fib_minus_2 = fib_minus_1
fib_minus_1 = fib
fib = fib_minus_1 + fib_minus_2
offset = -1
while fib > 1:
i = min(offset + fib_minus_2, len(self.phonebook) - 1)
if self.phonebook[i][0] < name:
fib = fib_minus_1
fib_minus_1 = fib_minus_2
fib_minus_2 = fib - fib_minus_1
offset = i
elif self.phonebook[i][0] > name:
fib = fib_minus_2
fib_minus_1 = fib_minus_1 - fib_minus_2
fib_minus_2 = fib - fib_minus_1
else:
return i
if fib_minus_1 and self.phonebook[offset + 1][0] == name:
return offset + 1
return -1
def search_friend(self, name):
index = self.fibonacci_search(name)
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: ")
phonebook.search_friend(friend_name)
# 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): Vatsal Mobile Number: 3495850448 Name (or 'exit' to stop): Sarah Mobile Number: 4545657658 Name (or 'exit' to stop): Sarvesh Mobile Number: 2020202020 Name (or 'exit' to stop): Vinod Mobile Number: 3243343434 Name (or 'exit' to stop): exit Phonebook: Name: Sarah Mobile Number: 4545657658 Name: Sarvesh Mobile Number: 2020202020 Name: Vatsal Mobile Number: 3495850448 Name: Vinod Mobile Number: 3243343434 Search for a friend: Enter the name of the friend: Vinod Friend found in the phonebook: Name: Vinod Mobile Number: 3243343434 Add a friend if not present: Enter the name of the friend: Tanaya Enter the mobile number: 3003002034 Phonebook: Name: Sarah Mobile Number: 4545657658 Name: Sarvesh Mobile Number: 2020202020 Name: Tanaya Mobile Number: 3003002034 Name: Vatsal Mobile Number: 3495850448 Name: Vinod Mobile Number: 3243343434 Process finished with exit code 0
Explanation:
In this program, the Phonebook class is defined to store friends’ names and mobile numbers. It includes a method add_friend to add a friend to the phonebook, a method fibonacci_search for performing a Fibonacci search on the phonebook, a method search_friend to search for a friend in the phonebook, and a method display_phonebook to display the phonebook.
The program prompts the user to enter the details of their friends, such as the name and mobile number. The input is taken until the user enters “exit” as the name.
After adding friends, the program displays the phonebook.
Next, the user is prompted to search for a friend by entering the friend’s name.
Finally, the user is prompted to add a friend if they are not already present in the phonebook. The user is asked to enter the friend’s name and mobile number.
The updated phonebook is then displayed.