Question: Anyone can correct my code by python? Define a function named generate_histogram(a_dict) which takes a dictionary as a parameter and prints a histogram. The key

 Anyone can correct my code by python? Define a function namedgenerate_histogram(a_dict) which takes a dictionary as a parameter and prints a histogram.

Anyone can correct my code by python?

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 d |x For example: Test Result |XXXXXXXXXXXXXXXXXXX data = {'x' : 19} generate_histogram(data) Answer: (penalty regime: 0 %) 1 def generate_histogram(a_dict): 2 a_dict = sorted(a_dict.items(), key-lambda item: item[1]) 3 a_dict.reverse() a_dict=dict(a_dict) 5 bar_length=4 6 for i in a dict: 7 print(i,bar_length*" "+"X"*a_dict[i]) 4 Precheck Check Test Expected Got | XXXXXXXXXXXXXXXXXXX X |XXXXXXXXXXXXXXXXXXX data = {'x' : 19} generate_histogram(data) Your code failed one or more hidden tests. Your code must pass all tests to earn any marks. Try again

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!