In: Computer Science
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
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: