In this assignment, you will write a program that generates text-based bar charts for simple data sets.
Your program should be named binner.py
.
You program will read in text files with a simple format.
The first line will be a string describing the data in the file.
Each line therafter will have a single integer number on it.
Below is a simple test example of such a file:
Test Data
1
2
3
4
5
6
7
8
9
10
1
3
5
7
9
3
7
Notice that there is one 2, 4, 6, 8, and 10. There are two 1s, 5s, and 9s. There are three 3s and 7s.
The first this your program should do is ask the user to enter the name of the input file. After doing this, your program will request a number of bins to chunk the data into. After the program has these two peices of information, you should use dictionaries (and lists) to organize the data, and then print out a corresponding bar-chart in text.
Below is a run using this example text file:
Enter data file name: test.txt
Enter number of bins: 10
-------------------------
Test Data
-------------------------
| # # |
| # # # # # |
| # # # # # # # # # # |
-------------------------
min = 1
max = 10
-------------------------
Notice that:
In this example, I told the program to bin (group) the data into 10 chunks.
Since there are 10 unique numbers in the data set, ech numbers gets it’s own bin!
Notice that in the first bar, there are two #
s, since there are two 1s in the data set.
The second bar has only one #
, because there is only one 2.
The third bar has three #
s because there are three 3s.
Basically, the bar at each chunk should correspond to how many elements are in each bin.
I could run the program with the same data set but with a different number of bins, for example, 5:
Enter data file name: test.txt
Enter number of bins: 5
---------------
Test Data
---------------
| # # |
| # # # # # |
| # # # # # |
| # # # # # |
---------------
min = 1
max = 10
---------------
Notice how the bar chart changed.
Since I specified 5 bins, there are only 5 bars.
The first bin contains all of the 1s and 2s, so the bar is 3 #
s tall.
The second bin contains the 3s and 4s, so it is 4 #
s tall.
The third bin contains the 5s and 6s, so it is 3 #
s tall.
And so on, for the remaining data.
Later on in this spec, I include several examples of running binner.py
with other data sets.
The “size” of each bin is calculated based on the data and on the number of bins. Each bin has a minimum and maximum value that can be in the bin. The minimum value should be inclusive. The maximum value should be exclusive. In other words, if a value in the data set falls right on the border between two bins, the number should be placed in the higher bin, not the lower one. The one exception to this rule is the highest bin, in which case the maximum value should be placed in it.
This assignment is due Monday, March 26th, at 5:00pm. Turn it in to the D2L drop box before the due date/time.
The tests and examples in this spec use several data sets, available for download:
Enter data file name: grades.txt
Enter number of bins: 10
-------------------------
Class exam grades
-------------------------
| # # |
| # # |
| # # |
| # # # |
| # # # # # |
| # # # # # |
| # # # # # # # |
| # # # # # # # # # # |
-------------------------
min = 0
max = 100
-------------------------
Enter data file name: grades.txt
Enter number of bins: 20
---------------------------------------------
Class exam grades
---------------------------------------------
| # # |
| # # # |
| # # # # # # |
| # # # # # # # # # |
| # # # # # # # # # # # # # # # # |
---------------------------------------------
min = 0
max = 100
---------------------------------------------
Enter data file name: tucson-weather.txt
Enter number of bins: 8
---------------------
Weekly Temp Averages (Tucson)
---------------------
| # |
| # |
| # # |
| # # # # # |
| # # # # # |
| # # # # # # # |
| # # # # # # # # |
| # # # # # # # # |
| # # # # # # # # |
| # # # # # # # # |
---------------------
min = 48
max = 93
---------------------
Enter data file name: tucson-weather.txt
Enter number of bins: 12
-----------------------------
Weekly Temp Averages (Tucson)
-----------------------------
| # # # |
| # # # # # |
| # # # # # |
| # # # # # # # |
| # # # # # # # # # # |
| # # # # # # # # # # # |
| # # # # # # # # # # # # |
-----------------------------
min = 48
max = 93
-----------------------------