Question: Write in python please and include explanations and screenshot Question 1 a) Define a function named create_histogram(dictionary) with a dictionary as a parameter to print
Write in python please and include explanations and screenshot
Question 1 a)
Define a function named create_histogram(dictionary) with a dictionary as a parameter to print 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 - Print items in descending order of value i.e. the number of 'X' characters - The width of the label is 7
data ={'c' : 3, 'b' : 4, 'a' : 2, 'd' : 1} create_histogram(data) then the output should be:
b |XXXX c |XXX a |XX d |X
| Test | Result |
data = {'x' : 19} create_histogram(data) | x |XXXXXXXXXXXXXXXXXXX |
Question 1 b)
In the previous question, an X was used to represent every integer value. However, we don't have space to represent large values using the approach (e.g., it would be unreadable if the values were in the hundreds or thousands). Therefore, we need to scale the bars so that each X represents a larger quantity. We will display the bars as percentages, where the largest value in the data set is treated as 100%. The height of the other bars will be proportional to the largest value in the dictionary.
Modify the create_histogram(dictionary) function defined in the previous question so that it has a second parameter (max_bar_length). This value will be used to determine the scale that we will apply when drawing the bars. For example, consider the following code fragment:
data = {'A': 94, 'B': 35, 'C': 87} max_bar_length = 10 create_histogram(data,max_bar_length) then the output should be:
A |XXXXXXXXXX C |XXXXXXXXX B |XXXX
To calculate the number of "X" characters to print, divide the value by the largest value in the dictionary and multiply by the max_bar_length parameter. Note: You should round to the nearest integer value when you print out the number of Xs. You can assume that the get_maximum_value() is given. The width of the label is 10.
| Test | Result |
data = {'Australia': 20126, 'Finland': 17120, 'NewZealand': 522, 'Singapore': 14552, 'Thailand': 806, 'Uruguay': 4784, 'Taiwan': 228} create_histogram(data, 50) | Australia |XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX Finland |XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX Singapore |XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX Uruguay |XXXXXXXXXXXX Thailand |XX NewZealand|X Taiwan |X |
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
