Answered step by step
Verified Expert Solution
Question
1 Approved Answer
My code cannot pass all tests,plz use python thanks Define a function named generate_histogram(a_dict) which takes a dictionary as a parameter and prints a histogram.
My code cannot pass all tests,plz use python thanks
Define a function named generate_histogram(a_dict) which takes a dictionary as a parameter and prints a histogram. The key of the dictionary is a string and the value is an integer. For each item, the function should print out the label (the key), followed by a vertical bar, and followed by the corresponding number of X characters. The items should be printed in descending order of value (i.e. the number of 'X' characters). The width of the label is 7. For example, consider the following code fragment: data = { 'c': 3, 'b' : 4, 'a': 2, 'd' : 1} generate_histogram(data) then the output should be: b | XXXX | XXX |xx 1x d For example: Test Result |XXXXXXXXXXXXXXXXXXX data = {'x' : 19} generate_histogram(data) import operator def generate_histogram(a_dict): a= sorted(a_dict.items (), key=operator.itemgetter(1), reverse=True) bar_length = 4 for i in range(len(a)): print(a[i][0],' '*bar_length, 'T', end='') print(a[i][1]*"X")Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started