Question

In: Computer Science

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

Solutions

Expert Solution

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))


Related Solutions

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 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 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...
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,...
Write a Python function with prototype “def anagramdictionary(wordlist):” that will return an “anagram dictionary” of the...
Write a Python function with prototype “def anagramdictionary(wordlist):” that will return an “anagram dictionary” of the given wordlist  An anagram dictionary has each word with the letters sorted alphabetically creating a “key”.
In Python write a function with prototype “def dictsort(d):” which will return a list of key-value...
In Python write a function with prototype “def dictsort(d):” which will return a list of key-value pairs of the dictionary as tuples (key, value), reverse sorted by value (highest first) and where multiple keys with the same value appear alphabetically (lowest first).
(True or False) The following function will compile. void print(int num) { return num+1; } Group...
(True or False) The following function will compile. void print(int num) { return num+1; } Group of answer choices True False Flag this Question Question 2 10 pts (True or False) The following code will output "I was true". bool isGreater(string s1, string s2) { if(s1 > s2) { return true; } return false; } int main() { if(isGreater("before","back")) { cout << "I was true"; } else { cout << "I was false"; } return 0; } Group of answer...
Using Python def specialAverage():    Print the number of 0s and then return the average of...
Using Python def specialAverage():    Print the number of 0s and then return the average of either the positive values in the list (if the optional parameter has value 1) or the negative values in the list (if the optional parameter has value 0).    Arguments: a list of numbers : the list can contain any numeric values an integer : this integer is an optional parameter and only takes value 0 or 1, and defaults to 1 if the...
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):
2. working with databases and files in python a) Write a function with prototype “def profound():”...
2. working with databases and files in python a) Write a function with prototype “def profound():” that will prompt the user to type something profound. It will then record the date and time using the “datetime” module and then append the date, time and profound line to a file called “profound.txt”. Do only one line per function call. Use a single write and f-string such that the file contents look like: 2020-10-27 11:20:22 -- Has eighteen letters does 2020-10-27 11:20:36...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT