you know all the n friends,have all the phone numbers and can give anyone any phone number.thus, you can always add any relation you want. you have to determine the minimum number of relations to add so that all friends can reach the sick one. input:the first line contains an integer,n, denoting the number of friends python code
SOLUTION FOR THE ABOVE QUESTION
SAMPLE PROGRAM OUTPUT
This solution allows a user to enter the number of friends he/she has, and then
enters the values for names and phone numbers for all the friends and then asks the user to enter the name of the sick friend then notifies the other friends that their friend is sick and give them his/her phone number to connect to him/her
SOLUTION CODE
#prompt the to enter the number of friends n
number_of_friends = int(input("Enter the number of friends(n): "))
#declare a list to store all the friends deatails stored in dictionary
#we will have a list containing dictionaries
my_friends_list = []
#propmt the user to enter the friends names and the phone numbers
#Storing them in a list
for i in range(0,number_of_friends):
#declare a dictionary to take the name and phone number of every employee
my_friends_dictionary = {}
#propmt the user to enter the name(string type)
friend_name = input("Enter the name of friend "+str(i+1)+": ")
#propmt the user to enter the phone number(string type)
phone_number = input("Enter the phone number of friend "+str(i+1)+": ")
#add the details in the dictionary
my_friends_dictionary["name"] = friend_name
my_friends_dictionary["phone_number"] = phone_number
#Add the dictionary to your list
my_friends_list.append(my_friends_dictionary)
#propmt the user to enter the name of the sick friend among the friends
sick_friend_name = input("Enter the name of the sick friend: ")
#Lets get the phone number for this sick friend
sick_friend_phone_number = "0"
for i in range(0, number_of_friends):
if my_friends_list[i]["name"] == sick_friend_name:
sick_friend_phone_number = my_friends_list[i]["phone_number"]
break
#notify every friend that their friend is sick showing them the contact
for i in range(0, number_of_friends):
if my_friends_list[i]["name"] != sick_friend_name:
print("Dear "+my_friends_list[i]["name"]+" your friend "+sick_friend_name
+ " having the phone number "+sick_friend_phone_number+" is sick")
Comments
Leave a comment