def drawhistogram(hist):
""" Draws a histogram of the given Dictionary's keys """
for i in sorted(hist.keys()):
print(i, end=': ')
if i < 10:
print(' ', end='')
print('x'*hist[i])
def histogram(nums):
""" Creates a Dictionary from a List of integers """
d = dict()
for num in nums:
d[num] = d.get(num,0) + 1
return d
def readints(filename):
""" Reads all integers from a file into a List """
nums = list()
with open('nums.txt') as fh:
for line in fh:
parts = line.split()
for part in parts:
nums.append(int(part))
return nums
# Read
nums = readints('nums.txt')
# Histogram
hist = histogram(nums)
# Draw
drawhistogram(hist)