Question

In: Computer Science

Python pls 1. The function only allow having one return statement. Any other statements are not...

Python pls

1. The function only allow having one return statement. Any other statements are not allowed. You only have to have exactly one return statement.

For example

the answer should be

def hello(thatman):

return thatman * thatman

1. Create a function search_wholo function. This function returns a list of 2 tuples, which people and their excitement skills, and sorted by decreasing excitement level(highest excitement skill first). If two people have the same excitement level, they should appear alphabetically. The list includes people who have the same hobby as an argument, and greater than or equal to the excitement level(which argument also).

For example

database = {'Dio': {'Lottery': 2, 'Chess': 4, 'Game': 3},'Baily': {'Game': 2, 'Tube': 1, 'Chess': 3}, 'Chico': {'Punch': 2, 'Chess': 5}, 'Aaron': {'Chess': 4, 'Tube': 2, 'Baseball': 1}}

def search_wholo(database, hobby:str, num: int):

# ONLY ONE STATEMENT ALLOWED (RETURN STATEMENT)

search_wholo(database, 'Chess',4) -> [('Chico', 5),('Aaron', 4), ('Dio',4)]

search_wholo(database, 'Game',0) -> [('Dio', 3), ('Baily', 2)]

search_wholo(database, 'Tube',3) -> [] #because all people's excitement skill for tube is lower than 3

search_wholo(database, 'Gardening',3) -> [] #because there is no "gardening" hobby in the dictionary

Solutions

Expert Solution

Code:

def search_wholo(database, hobby: str, num: int):

# We iterate over every pair of key and value in the database dict

# If hobby is in the hobbies of a person and the excitement level of person is greater than or equal to num

# Then we store the tuple (name, hobbies[hobby]) of the person

# Then we sort the list of tuples as

# first sorting the list based on excitement level in decreasing order

# then sorting the list based on name in increasing lexographic order

return sorted([(name, hobbies[hobby]) for name, hobbies in database.items() if hobby in hobbies and hobbies[hobby] >= num], key=lambda x: (-x[1], x[0]))


def main():

database = {'Dio': {'Lottery': 2, 'Chess': 4, 'Game': 3},

'Baily': {'Game': 2, 'Tube': 1, 'Chess': 3},

'Chico': {'Punch': 2, 'Chess': 5},

'Aaron': {'Chess': 4, 'Tube': 2, 'Baseball': 1}}

print(search_wholo(database, 'Chess', 4))

print(search_wholo(database, 'Game', 0))

print(search_wholo(database, 'Tube', 3))

print(search_wholo(database, 'Gardening', 3))


main()

Code Screenshot:

Output:


Related Solutions

Python pls 1. The function only allow having one return statement. Any other statements are not...
Python pls 1. The function only allow having one return statement. Any other statements are not allowed. You only have to have exactly one return statement. (ONE STATEMENT ALLOWED) For example the answer should be def hello(thatman): return thatman * thatman Create a function search_hobby. This function returns a set of hobby names and people's excitement level. input database = {'Dio': {'Lottery': 2, 'Chess': 4, 'Game': 3},'Baily': {'Game': 2, 'Tube': 1, 'Chess': 3}, 'Chico': {'Punch': 2, 'Chess': 5}, 'Aaron': {'Chess':...
Python pls 1. The function only allow having one return statement. Any other statements are not...
Python pls 1. The function only allow having one return statement. Any other statements are not allowed. You only have to have exactly one return statement. For example the answer should be def hello(thatman): return thatman * thatman Create a function search_hobby. This function returns a set of hobby names and people's excitement level. input database = {'Dio': {'Lottery': 2, 'Chess': 4, 'Game': 3},'Baily': {'Game': 2, 'Tube': 1, 'Chess': 3}, 'Chico': {'Punch': 2, 'Chess': 5}, 'Aaron': {'Chess': 4, 'Tube': 2,...
PYTHON PLS 1) Create a function search_by_pos. This function only has one return statement. This function...
PYTHON PLS 1) Create a function search_by_pos. This function only has one return statement. This function returns a set statement that finds out the same position and same or higher skill number. This function searches the dictionary and returns the same position and same or higher skill level. The function output the set statements that include the position only. For example input : dict = {'Fiora': {'Top': 1, 'Mid': 4, 'Bottom': 3},'Olaf': {'Top': 3, 'Mid': 2, 'Support': 4},'Yasuo': {'Mid': 2,...
Python ONLY ONE STATEMENT ALLOWED PER FUNCTION (ONE RETURN STATEMENT ALLOWED) def plsfollowrule(num): return num like...
Python ONLY ONE STATEMENT ALLOWED PER FUNCTION (ONE RETURN STATEMENT ALLOWED) def plsfollowrule(num): return num like this. 1) create a function popular. The dictionary called database contains each people's hobby and their excitement level. This function searches the dictionary and returns a list of that most popular hobby to least popular hobby. If multiple hobbies have the same number of popularity, follow alphabetical order. #What is popular means? The dictionary contains each people's hobby. The programmer should search the dictionary...
Write a python function that will return the total length of line that passes through any...
Write a python function that will return the total length of line that passes through any number of provided points ( (x,y) ). The points should be passed as individual tuples or lists. The function should also have a parameter (True or False) to indicate whether the line should start from the origin, and that parameter should default to False. If True, the returned value should include the distance from the origin to the first point, otherwise start adding distances...
Python pls Create a function dict_sum. This function takes a dictionary and sums up the values...
Python pls Create a function dict_sum. This function takes a dictionary and sums up the values in the dictionary. For example: dict1 = {1: {'una': 5, 'dos': 7, 'tres': 9, 'quar' : 11}, 2: {'dos':2, 'quar':4}, 3:{'una': 3, 'tres': 5}, 4:{'cin': 6}, 5:{'tres': 7 , 'cin': 8}} dict2 = {300:{'s': 300}, 400:{'s': 100, 'g': 100, 'p': 100}, 500: {'s': 50 ,'m': 400, 'p':30, 'i': 50}, 600: {'s': 40, 'i': 400}, 700: {'m': 100, 'p': 50}} def dict_sum(db): should give output...
complete in python The function sumEven should return the sum of only the even numbers contained...
complete in python The function sumEven should return the sum of only the even numbers contained in the list, lst. Example list_of_nums = [1, 5, 4, 8, 5, 3, 2] x = sum_evens(list_of_nums) print(x) #prints 14 Start your code with def evens(lst):
Python pls create a function called search_position. This function returns a list. team1 = {'Fiora': {'Top':...
Python pls create a function called search_position. This function returns a list. team1 = {'Fiora': {'Top': 1, 'Mid': 4, 'Bottom': 3},'Olaf': {'Top': 3, 'Mid': 2, 'Support': 4},'Yasuo': {'Mid': 2, 'Top': 5},'Shaco': {'Jungle': 4, 'Top': 2, 'Mid': 1}} def search_position(team1): returns [(5, [('Top', ['Yasuo'])]), (4, [('Mid', ['Fiora']), ('Support',['Olaf']), ('Jungle',['Shaco'])])   (3, [('Bottom', ['Fiora']), ('Top', ['Olaf'])]), (2, [('Mid', ['Olaf','Yasuo']), ('Top', ['Shaco'])]), (1, [('Mid', ['Shaco'])])]
Python pls create a function called search_position. This function returns a dictionary. team1 = {'Fiora': {'Top':...
Python pls create a function called search_position. This function returns a dictionary. team1 = {'Fiora': {'Top': 1, 'Mid': 4, 'Bottom': 3},'Olaf': {'Top': 3, 'Mid': 2, 'Support': 4},'Yasuo': {'Mid': 2, 'Top': 5},'Shaco': {'Jungle': 4, 'Top': 2, 'Mid': 1}} def search_position(team1): should return {'Top': {'Fiora': 1, 'Yasuo':5,'Olaf':3,'Shaco':}, 'Jungle': {'Shaco': 4}, 'Mid': {'Yasuo', 2, 'Fiora': 4,'Olaf':2}, 'Bottom': {'Fiora': 3}, 'Support': {'Olaf': 4}} *******IMPORTANT*************** The result should be alphabetical order.
Python pls Create a function party_freq(dicto:dict): this function returns a list inside tuple that how many...
Python pls Create a function party_freq(dicto:dict): this function returns a list inside tuple that how many times each person party in the day. For example def party_freq(dicto:dict) -> [(str,{(int,int,int): int})]: #code here input dict1 ={'fire1': {(2000,5,20,480) : ('Aaron', 25, 300, ( 0, 300)), (2000,5,20,720) : ('Baily', 45, 1500, (1500,500)), (2000,5,21,490) : ('Aaron', 35, 500, (1300,500)) }, 'fire2': {(2000,5,20,810) : ('Baily', 45, 1400, (600,1600)), (2000,5,20,930) : ('Baily', 43, 1800, ( 0, 0)) }} output [('Aaron', {(2000,5,20): 1, (2000,5,21): 1}), ('Baily', {(2000,5,20):...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT