Answered step by step
Verified Expert Solution
Link Copied!

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.

image text in transcribedimage text in transcribed

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design Application Development And Administration

Authors: Michael V. Mannino

3rd Edition

0071107010, 978-0071107013

More Books

Students also viewed these Databases questions

Question

How do Data Types perform data validation?

Answered: 1 week ago

Question

How does Referential Integrity work?

Answered: 1 week ago