In: Computer Science
(Python3)
Write a function friend_besties() that calculates the "besties" (i.e. degree-one friends) of a given individual in a social network. The function takes two arguments:
The function should return a sorted list, made up of all "degree-one" friends for the individual. In the instance that the individual does not have any friends in the social network, the function should return an empty list.
Example:
>>> friend_besties('kim', {'kim': {'sandy', 'alex',
'glenn'}, 'sandy': {'kim', 'alex'}, 'alex': {'kim', 'sandy'},
'glenn': {'kim'}})
['alex', 'glenn', 'sandy']
>>> friend_besties('ali', {'kim': {'sandy', 'alex',
'glenn'}, 'sandy': {'kim', 'alex'}, 'alex': {'kim', 'sandy'},
'glenn': {'kim'}})
[]
Note: Please indent the code as per the provide code screenshot. Otherwise, it raises indentation errors.
Program Screenshot:
friend_besties.py
Sample Output:
Run1:
Run2:
Code to be Copied:
#Define the function friend_besties
#of have parameters individual list, bestie_dict list
def friend_besties(individual,bestie_dict):
#declare temp list
temp_lst=[]
#use if condition to check the keys
#in each individual list
if individual in bestie_dict.keys():
#store the bestie_dict in temp list
temp_lst=list(bestie_dict[individual])
#sort the list
temp_lst.sort()
#return the list
return temp_lst