In: Computer Science
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 and figure out how many times this hobby popular to the people.
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 popular(database) -> list:
#ONE STATEMENT ALLOWED, THAT WILL BE ONE RETURN STATEMENT
output
['Chess', 'Game', 'Tube', 'Baseball', 'Lottery', 'Punch']
#Chess appear the most time which 4
#Game appear 2 times
#Tube appear 2 times but by the alphabetical order, it located after game
#baseball appear 1 time
#lottery appear 1 time but by the alphabetical order, it located after baseball
#punch appear 1 time but by the alphabetical order, it located after lottery
from collections import Counter,OrderedDict
from operator import itemgetter
# function to return list of most popular to least popular hobby
def popular(database):
return list(OrderedDict(sorted(OrderedDict(Counter(sorted([b for a in database.values() for b in a]))).items(), key=itemgetter(1,1), reverse=True)).keys())
# dictionary contains each people's hobby with excitement level
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(popular(database))