Question 21: 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. Download hole Program / Project code, by clicking following link: In if-else construct which part will be executed if condition is ture in Java ? In an if-else construct in Java, the code within the if block will be executed if the condition specified in the if statement is true. If the if condition is false, then the code within the else block (if an else block is provided) will be executed. Programming Code: Following code write in: DS_P21.py
# Python Program / Project
def FibSearch(dict1, key, n):
b = 0
a = 1
f = b + a
while(f < n):
b = a
a = f
f = b + a
offset = -1
while(f>1):
i = min(offset+b, n-1)
if(dict1[i]key):
f = b
a = a - b
b = f - a
else:
return i
if( a and dict1[offset+1] == key):
return offset + 1
return -1
# Driver code
dict1 = {}
while (True):
print("********** Menu **********")
print("1. Fibonacci Search.")
print("2. Exit.")
ch = int(input("Enter your choice: "))
if ch == 1:
num = int(input("Enter How many students: "))
for i in range(num):
data = input("Enter student name: ")
num = int(input("Enter student phone no: "))
dict1[num] = data
print("Student phone number and name: ")
print(dict1)
list1 = dict1.keys()
list1 = list(list1)
list1.sort()
print("Sorted List is: ",list1)
key = int(input("Enter phone number to search: "))
index = FibSearch(list1, key, num)
if index<0:
print("{} was not found.".format(key))
else:
print("{} was found at index {}.".format(key,index))
print("Details of numbers found: ", dict1[key])
elif ch == 2:
print("You have Exit.")
exit()
else:
print("Invalid choice.")
#Rahul Modi Mamta Abdul
#986, 125, 209, 937
# Thanks For Reading.
Copy Code
Output:
# Python Program / Project def FibSearch(dict1, key, n): b = 0 a = 1 f = b + a while(f < n): b = a a = f f = b + a offset = -1 while(f>1): i = min(offset+b, n-1) if(dict1[i]
key): f = b a = a - b b = f - a else: return i if( a and dict1[offset+1] == key): return offset + 1 return -1 # Driver code dict1 = {} while (True): print("********** Menu **********") print("1. Fibonacci Search.") print("2. Exit.") ch = int(input("Enter your choice: ")) if ch == 1: num = int(input("Enter How many students: ")) for i in range(num): data = input("Enter student name: ") num = int(input("Enter student phone no: ")) dict1[num] = data print("Student phone number and name: ") print(dict1) list1 = dict1.keys() list1 = list(list1) list1.sort() print("Sorted List is: ",list1) key = int(input("Enter phone number to search: ")) index = FibSearch(list1, key, num) if index<0: print("{} was not found.".format(key)) else: print("{} was found at index {}.".format(key,index)) print("Details of numbers found: ", dict1[key]) elif ch == 2: print("You have Exit.") exit() else: print("Invalid choice.") #Rahul Modi Mamta Abdul #986, 125, 209, 937 # Thanks For Reading. Copy Code
Output:
0 Comments