In: Computer Science
##### answer in python
#####picture of output and input
Ask the user to enter name, phonenumber, email for each customer. Build a dictionary of dictionaries to hold 10 customers with each customer having a unique customer id. (random number generated)
# The code for the problem is given below for indentation refer to pictures below and if have any problem then feel free to ask:
# importing random for generating random integers
import random
# making dictionary for details of each customer
dict = {}
# making dictionary for details of every customer with id
dictMajor = {}
idLis = []
# list for saving the keys of the dictionary
keyList = []
# making a dictionary of dictionaries with random generated id
# from 0 to 9 for 10 customers
def dictAssign(dictMajor, dict):
while 1:
d = random.randint(0, 10)
if d not in idLis:
idLis.append(d)
dictMajor[d] = dict
return
# function for sorting the keys using insertion sort
def insertSort(keyList, n):
for i in range(1, n):
temp = keyList[i]
j = i-1
while j >= 0 and temp < keyList[j]:
keyList[j+1] = keyList[j]
j = j - 1
keyList[j+1] = temp
# using binary search for searching the required id
def binarySearchId(keyList, first, last, id):
if last >= first:
mid = first+(last-first)//2
if keyList[mid] == id:
return mid
elif keyList[mid] < id:
return binarySearchId(keyList, mid+1, last, id)
else:
return binarySearchId(keyList, first, mid-1, id)
return -1
# asking the user to enter the data for the 10 custormer
for i in range(10):
print("\nEnter the details of Customer", i+1)
name = input("Enter the name: ")
phoneNumber = input("Enter the phone number: ")
email = input("Enter the Email: ")
dict['Name'] = name
dict['Phone Number'] = phoneNumber
dict['Email'] = email
dictAssign(dictMajor, dict)
# extracting the keys into a list
for value in dictMajor.keys():
keyList.append(value)
# calling the function insertion sort
n = len(keyList)
insertSort(keyList, n)
# asking the user to enter the id to search and searching the id in keylist
# using binarySearchId() function
id = int(input("Enter the customer id to search: "))
index = binarySearchId(keyList, 0, n-1, id)
# giving the result of search to user
if index == -1:
print("Customer with entered id not found!!")
else:
print("Customer with id:", id,
"is found\nThe details of customer are:", dictMajor[id])
OUTPUT: