Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

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
Check Mark
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
Knowledge Booster
Background pattern image
Similar questions
    SEE MORE QUESTIONS
    Recommended textbooks for you
    Text book image
    Database System Concepts
    Computer Science
    ISBN:9780078022159
    Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
    Publisher:McGraw-Hill Education
    Text book image
    Starting Out with Python (4th Edition)
    Computer Science
    ISBN:9780134444321
    Author:Tony Gaddis
    Publisher:PEARSON
    Text book image
    Digital Fundamentals (11th Edition)
    Computer Science
    ISBN:9780132737968
    Author:Thomas L. Floyd
    Publisher:PEARSON
    Text book image
    C How to Program (8th Edition)
    Computer Science
    ISBN:9780133976892
    Author:Paul J. Deitel, Harvey Deitel
    Publisher:PEARSON
    Text book image
    Database Systems: Design, Implementation, & Manag...
    Computer Science
    ISBN:9781337627900
    Author:Carlos Coronel, Steven Morris
    Publisher:Cengage Learning
    Text book image
    Programmable Logic Controllers
    Computer Science
    ISBN:9780073373843
    Author:Frank D. Petruzella
    Publisher:McGraw-Hill Education