Question

I have a dictionary in which has strings for keys and integers for values. I need...

I have a dictionary in which has strings for keys and integers for values. I need to count the length of each key and place it in one of three categories, small, medium or large. Those are defined by:

  • small: length 0-4, inclusive.
  • medium: length 5-7, inclusive.
  • large: length 8+, inclusive.

After they are they have been sorted into their appropriate category, the program needs to check which word of each small, medium, large category had the largest key.

Sample of my dictionary:

freq_dict= {'To': 13, 'take': 7, 'each': 7, 'day': 7, 'as': 8, 'it': 8, 'comes': 9, 'gratefully': 1, 'praise': 1, 'The': 1, 'One': 1}

In Python, please.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Python code:

#given dictionary

freq_dict={'To':13,'take':7,'each':7,'day':7,'as':8,'it':8,'comes':9,'gratefully':1,'praise': 1,'The':1,'One':1}

#dictionary to store small words

small={}

#dictionary to store medium words

medium={}

#dictionary to store large words

large={}

#loop for categorizing words

for i in freq_dict:

    #checking if small

    if(freq_dict[i]<=4 and freq_dict[i]>=0):

        #adding to small

        small[i]=freq_dict[i]

    #checking if medium

    elif(freq_dict[i]<=7 and freq_dict[i]>=5):

        #adding to medium

        medium[i]=freq_dict[i]

    #checking if large

    elif(freq_dict[i]>=8):

        #adding to large

        large[i]=freq_dict[i]

#initializing max value of small

max_val1=-1

#looping each value in small

for i in small:

    #checking for Largest value

    if(small[i]>=max_val1):

        #obtaining Largest value

        max_val1=small[i]

#obtaining all words having Largest value

small_word=[i for i in small if small[i]==max_val1]

#printing all Largest words in small

print("Largest key in small: ",end="")

print(*small_word,sep=",")

#initializing max value of medium

max_val2=-1

#looping each value in medium

for i in medium:

    #checking for Largest value

    if(medium[i]>=max_val2):

        #obtaining Largest value

        max_val2=medium[i]

#obtaining all words having Largest value

medium_word=[i for i in medium if medium[i]==max_val2]

#printing all Largest words in medium

print("Largest key in medium: ",end="")

print(*medium_word,sep=",")

#initializing max value of large

max_val3=-1

#looping each value in large

for i in large:

    #checking for Largest value

    if(large[i]>=max_val3):

        #obtaining Largest value

        max_val3=large[i]

#obtaining all words having Largest value

large_word=[i for i in large if large[i]==max_val3]

#printing all Largest words in large

print("Largest key in large: ",end="")

print(*large_word,sep=",")

Screenshot:

1 #given dictionary 2 freq_dict={To:13, take:7,each:7, day:7,as:8,it:8,comes:9,gratefully:1, praise: 1, Th

46 38 #Looping each value in medium 39 - for i in medium: 40 #checking for Largest value 41 if(medium[i]>=max_va12): 42 #obta

Output:

Largest key in small: praise, gratefully, One, The Largest key in medium: take, day, each Largest key in large: To

Add a comment
Know the answer?
Add Answer to:
I have a dictionary in which has strings for keys and integers for values. I need...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Python 5. Write a function named grade_components that has one parameter, a dictionary. The keys for...

    Python 5. Write a function named grade_components that has one parameter, a dictionary. The keys for the dictionary are strings and the values are lists of numbers. The function should create a new dictionary where the keys are the same strings as the original dictionary and the values are tuples. The first entry in each tuple should be the weighted average of the non-negative values in the list (where the weight was the number in the 0 position of the...

  • USING PYTHON! a) Programmatically (do not just write a definition!) create a dictionary whose keys are...

    USING PYTHON! a) Programmatically (do not just write a definition!) create a dictionary whose keys are the integers 1 through 10 and whose respective values are the keys cubed (e.g. a key of 5 has a value of 125). b) Write a function that, given a user inputted string, outputs a dictionary where each letter is a key and each value is the letter’s position in the string. Note that you ​must handle cases where a word could contain the...

  • Language: Python Topic: Dictionaries Function name: catch_flight Parameters: dictionary, tuple containing two strings Returns: dictionary Description:...

    Language: Python Topic: Dictionaries Function name: catch_flight Parameters: dictionary, tuple containing two strings Returns: dictionary Description: You’ve been stuck in NYC for around 8 months all by yourself because you haven’t been able to find a good time to fly home. You’re willing to go to any city but want to see how many flights to each location fit your budget. You’re given a dictionary that has city names (strings) as the keys and a list of prices (list) and...

  • 1. Define the draw_histogram() function which is passed a Python dictionary as a parameter. The keys...

    1. Define the draw_histogram() function which is passed a Python dictionary as a parameter. The keys of the dictionary are single letters and the corresponding values are integers, e.g., {'b': 5, 'a': 6, 'c': 3}. For each key:value pair in the dictionary the function prints the key, followed by ": ", followed by a series of stars. The number of stars printed is given by the value corresponding to the key. The keys are printed in alphabetical order. Note that...

  • In python, PART A: I am trying to get a dictionary with size(4, 5, 6) as...

    In python, PART A: I am trying to get a dictionary with size(4, 5, 6) as keys and an array for key containing a list of values (words from file of respective size) associated with those keys I am reading from a file of strings, where I am only interested with words of length 4, 5, and 6 to compute my program reading the text file line by line: At first, I have an empty dictionary then to that I...

  • For Python [25 pts] Write the method divisorList(aList, divisor) that takes in a list with elements...

    For Python [25 pts] Write the method divisorList(aList, divisor) that takes in a list with elements of any data type and a divisor which is in the form of an integer. The output is a sorted list of only the integers that are divisible by the divisor. Example: divisorList([1, 1, 12, 'a', 90, 34], 2) will return [12, 34, 90] since 12, 90, and 34 are all divisible by two. Note that the returned list is sorted from smallest to...

  • Python 3, nbgrader, pandas 0.23.4 Q2 Default Value Functions (1 point) a) Sort Keys Write a function called sort_keys, which will return a sorted version of the keys from an input dictionary Input(s...

    Python 3, nbgrader, pandas 0.23.4 Q2 Default Value Functions (1 point) a) Sort Keys Write a function called sort_keys, which will return a sorted version of the keys from an input dictionary Input(s): dictionary :dictionary reverse boolean, default: False Output(s): .sorted_keys: list Procedure(s) Get the keys from the input dictionary using the keys()method (this will return a list of keys) Use the sorted function to sort the list of keys. Pass in reverse to sorted to set whether to reverse...

  • I need help writing these : Given a dictionary gradeCounts = { "A": 8, "D": 3,...

    I need help writing these : Given a dictionary gradeCounts = { "A": 8, "D": 3, "B": 15, "F": 2, "C": 6} write the Python statement(s) to print: a. all the keys. b. all the values. c. all the key and value pairs. d. all of the key and value pairs in key order. e. the average value. f. a chart similar to the following in which each row contains a key followed by a number of asterisks equal to...

  • Hey guys I need help with this assignment. However it contains 7 sub-problems to solve but I figured only 4 of them can...

    Hey guys I need help with this assignment. However it contains 7 sub-problems to solve but I figured only 4 of them can be solved in one post so I posted the other on another question so please check them out as well :) Here is the questions in this assignment: Note: Two helper functions Some of the testing codes for the functions in this assignment makes use of the print_dict in_key_order (a dict) function which prints dictionary keyvalue pairs...

  • PYTHON Define the remove_e_synonyms() function which is passed a dictionary as a parameter. The keys of...

    PYTHON Define the remove_e_synonyms() function which is passed a dictionary as a parameter. The keys of the parameter dictionary are words and the corresponding values are lists of synonyms (synonyms are words which have the same or nearly the same meaning). The function removes all the synonyms which contain the letter 'e' or 'E') from each corresponding list of synonyms. As well, the function sorts each corresponding list of synonyms. Note: the testing code makes use of the print_dict_in_key_order(a_dict) which...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT