-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinal.py
More file actions
71 lines (62 loc) · 2.1 KB
/
final.py
File metadata and controls
71 lines (62 loc) · 2.1 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/local/bin/python3
""" Prints a table showing a word count for each of the word lengths encountered. """
def split_file_into_words(filename):
""" Splits a file into words and returns them in a list.
>>> len(split_file_into_words("declaration.txt"))
1322
"""
f = open(filename, 'r')
words = []
for line in f.readlines():
for word in line.split():
words.append(word)
f.close()
return words
def measure_word_length(word):
""" Measures the length of a word without punctuation marks.
>>> measure_word_length("&there!")
5
>>> measure_word_length("Hi?")
2
"""
punctuation = ('!','@','#','$','%','^','&','*','(',')','-','+','-','=','[',']','{','}','\\','|',';',"'",':','"',',','.','/','<','>','?')
wlength = 0
for c in word:
if c in punctuation:
pass
else:
wlength += 1
return wlength
def create_word_length_dict(filename):
""" Builds a dictionary of word lengths.
>>> create_word_length_dict("declaration.txt")
{1: 16, 2: 267, 3: 267, 4: 169, 5: 140, 6: 112, 7: 99, 8: 68, 9: 61, 10: 56, 11: 35, 12: 13, 13: 9, 14: 7, 15: 2}
"""
lengths = {}
words = split_file_into_words(filename)
# Tally word lengths
for word in words:
wlength = measure_word_length(word)
current_entry = lengths.get(wlength, None)
if current_entry:
lengths[wlength] += 1
else:
lengths[wlength] = 1
# Disregard words of zero length
del lengths[0]
return lengths
def _test():
import doctest, final
return doctest.testmod(final)
if __name__ == "__main__":
import sys
try:
filename = sys.argv[1]
lengths_dict = create_word_length_dict(filename)
print("Length Count")
for length, count in lengths_dict.items():
print("{0} {1}".format(length, count))
except IOError:
print("Unable to process your request. Please verify that the filename provided is correct.")
except IndexError:
print("Please provide a filename when running this program: './final.py filename.text'")