Skip to content

Instantly share code, notes, and snippets.

@accessnash
Created October 19, 2015 07:28
Show Gist options
  • Select an option

  • Save accessnash/bbd7456080e202d5ffb0 to your computer and use it in GitHub Desktop.

Select an option

Save accessnash/bbd7456080e202d5ffb0 to your computer and use it in GitHub Desktop.
"""Creates a histogram of the word lengths from the text file"""
import sys
def read_text():
inputF = open(sys.argv[1], 'r')
return inputF
openF = read_text()
text = openF.read()
for punc in "&,?-;:.'":
text = text.replace(punc, "")
words = text.lower().split()
lngth_of_words = []
for word in words:
lngth_of_words.append(len(word))
max_l = max(lngth_of_words)
freqs = [0 for i in range(max_l + 1)]
for length in lngth_of_words:
freqs[length] += 1
print("Length Count")
for i, count in enumerate(freqs):
if i != 0:
print("{:>4} {:>8}".format(i, count))
freq = dict([(i, count) for i, count in enumerate(freqs)])
yL = round(max(freqs) + 100, -2)
for y in range(yL, 9, -20):
if y % 100 ==0:
print("{:>8}-| ".format(y), end ='')
else:
print("{:>8} | ".format(''), end ='')
for x in range(1, max_l +2):
if freq.get(x,0) >= y:
column = "***"
else:
column = " "
print(column, end="")
print()
print(' -+-+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+-')
print(' | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment