in python create function that has a parameter of dict[str, str] key should be a name value should be a word when given a dict of names and word, function should return the word that is most common ******************************* no using key, value for loops no .get, .items, .defaultset no importing builtins no using .values, .keys, or .index must do it through dictionary cycling ************************** should return a string
in python create function that has a parameter of dict[str, str] key should be a name value should be a word when given a dict of names and word, function should return the word that is most common ******************************* no using key, value for loops no .get, .items, .defaultset no importing builtins no using .values, .keys, or .index must do it through dictionary cycling ************************** should return a string
Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
Related questions
Question
in python
create function that has a parameter of dict[str, str]
key should be a name
value should be a word
when given a dict of names and word, function should return the word that is most common
*******************************
no using key, value for loops
no .get, .items, .defaultset no importing builtins
no using .values, .keys, or .index
must do it through dictionary cycling
**************************
should return a string
Expert Solution
The Code:
def mostFreq(dictA): # function to return most frequent value in a dictionary
f_dict = {} # an empty dictionary
for k in dictA: # for each key in dictionary
if dictA[k] in f_dict: # if value in f_dict
f_dict[dictA[k]] += 1 # then increment the count by 1
else: # else, (i.e. value not in f_dict)
f_dict[dictA[k]] = 1 # then, initialize the count to 1
maximum = 0 # variable initialization
for key in f_dict: # for each key in f_dict
if maximum < f_dict[key]: # if maximum < f_dict[key] (i.e. value)
maximum = f_dict[key] # then, assign that value to maximum
for key in dictA: # for each key in the dictionary
if f_dict[dictA[key]] == maximum: # if it matches with maximum value
return dictA[key] # return the word
# main program
n = int(input("Enter the number of items you want to enter into the dictA: ")) # taking value of n from user
dict1 ={} # empty dictA
for i in range(n): # for loop runs n times
name = input("Enter name: ") # taking name from user
dict1[name] = input("Enter word: ") # taking word from user
print(dict1) # display the dictA
print("Most Frequent word is =", mostFreq(dict1)) # display output
Step by step
Solved in 3 steps with 2 images
Recommended textbooks for you
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education