-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuildhist.py
41 lines (36 loc) · 1.11 KB
/
buildhist.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# Prints a histogram for a distribution of letter grades computed
# from a collection of numeric grades extracted from a text file.
from maphist import Histogram
def main():
gradeHist = Histogram( "ABCDEF" )
gradeFile = open( 'cs101grades.txt', 'r' )
for line in gradeFile:
grade = int(line)
gradeHist.incCount( letterGrade(grade) )
printChart( gradeHist )
print gradeHist.totalCount()
gradeFile.close()
# Determines the letter grade for the given numeric value.
def letterGrade( grade ):
if grade >= 90:
return 'A'
elif grade >= 80:
return 'B'
elif grade >= 70:
return 'C'
elif grade >= 60:
return 'D'
else:
return 'F'
# Prints the histogram as a horizontal bar chart.
def printChart( gradeHist ):
print " Grade Distribution"
letters = ('A', 'B', 'C', 'D', 'F')
for letter in letters:
freq = gradeHist.getCount( letter )
print ' |'
print letter + "+" + '*' * freq
print ' |'
print ' +----+----+----+----+----+----+----+'
print ' 0 5 10 15 20 25 30 35'
main()