Question

In: Computer Science

(Python3) Write a function friend_besties() that calculates the "besties" (i.e. degree-one friends) of a given individual...

(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:

  • individual, an individual in the social network, in the form of a string ID.
  • bestie_dict, a dictionary of sets of friends of each individual in the social network (as per the first question of the Project)

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'}})
[]

Solutions

Expert Solution

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


Related Solutions

python3 (3a) Write a function, frequent, with one parameter, psw, a string. If psw is in...
python3 (3a) Write a function, frequent, with one parameter, psw, a string. If psw is in a list of frequently used passwords ['password', '12345', 'qwerty', 'letmein', 'trustno1', '000000', 'passw0rd'], frequent should return False; otherwise, return True. Be sure to include at least three good test cases in the docstring. (3b) Password Protection SecuriCorp has recently been the victim of a number of security breaches. Internal analysis has determined that employees use simple passwords that are too easy to guess. You...
Read the function definitions of the two functions: “compute_st_ave” and “compute_quiz_ave”. Write a function that calculates...
Read the function definitions of the two functions: “compute_st_ave” and “compute_quiz_ave”. Write a function that calculates the average of student averages. (Student averages are stored in st_ave [5] array). Include a statement to print this average on the screen. Declare this function above the “main” function where other functions are declared. In the main program, invoke this function. //Reads quiz scores for each student into the two-dimensional array grade //(but the input code is not shown in this display). Computes...
Python3 *Use Recursion* Write a function called smallest_sum(A,L) where A is the value and L is...
Python3 *Use Recursion* Write a function called smallest_sum(A,L) where A is the value and L is a list of ints. smallest_sum should return the length of the smallest combinations of L that add up to A if there are no possible combinations then float("inf") is returned. Examples smallest_sum(5, [2, 3]) == 2 smallest_sum(313, [7, 24, 42]) == 10 smallest_sum(13, [8, 3, 9, 6]) == float(‘‘inf’’) No Loops and map/reduce functions are allowed
Write a program that contains a function that takes in three arguments and then calculates the...
Write a program that contains a function that takes in three arguments and then calculates the cost of an order. The output can be either returned to the program or as a side effect. 1. Ask the user via prompt for the products name, price, and quantity that you want to order. 2. Send these values into the function. 3. Check the input to make sure the user entered all of the values. If they did not, or they used...
python3 Continue from the previous function, write a draw_table method which draws a table of data....
python3 Continue from the previous function, write a draw_table method which draws a table of data. For example, consider the following code fragment: my_table = Table([['Alice', 24], ['Bob', 19]], width=10) my_table.draw_table() produces: |Alice 24 | |Bob 19 | Note: the column width is the value set up by the constructor. Insert a space in between columns. and my previous function is class Table: def __init__(self,x, h = None, width=5): self.x= x self.h = h self.w= width def __str__(self): return "headers={},...
Write a program that calculates the mean, median and mode of a given array in the...
Write a program that calculates the mean, median and mode of a given array in the following manner: i) Write three functions mean (), median () and mode () that calculates the mean, median and mode of an array. ii) Using these functions write a program that calculates all the three above mentioned values for two different arrays A and B where A is an array that contains the number of hours spent by a person each day for playing...
Write a function that implements the following formula to calculates the amount of financial assistance for...
Write a function that implements the following formula to calculates the amount of financial assistance for families in need: • If the annual family income is between $30,000 and $40,000 and the family has at least 3 children, the assistance amount will be $1,000 per child. • If the annual family income is between $20,000 and $30,000 and the family has at least 2 children, the assistance amount will be $1,500 per child. • If the annual family income is...
Write a MATLAB function, called arbpoly, that computes a polynomial arbitrary nth degree. The function will...
Write a MATLAB function, called arbpoly, that computes a polynomial arbitrary nth degree. The function will take 2 inputs: 1) the first input will be a row vector, c, containing the coefficients of the polynomial, starting with the coefficient of the highest - degree term; 2) the second input will be a scalar, x, which is a real number at which the polynomial will be evaluated. The function's only output, y, will be the scalar value of the polynomial computed...
Write out the production function. What do each of the variables in the function represent? (i.e....
Write out the production function. What do each of the variables in the function represent? (i.e. factors that determine productive capability) Which of these factors are key to long term sustainable growth?
Please solve the following problem for MATLAB Write a user-defined function that calculates the average and...
Please solve the following problem for MATLAB Write a user-defined function that calculates the average and the standard deviation of a list of numbers. Use the function to calculate the average and the standard deviation of the following list of grades : 80 75 91 60 79 89 65 80 95 50 81
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT