-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
59 lines (41 loc) · 1.41 KB
/
Copy pathmain.py
File metadata and controls
59 lines (41 loc) · 1.41 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
def main() :
book_path = "books/frankenstein.txt"
text = get_book_text(book_path)
words_count = get_words_count(text)
characters_count = get_characters_count(text)
characters_by_frequency_desc = get_characters_by_frequency_desc(characters_count)
print(f"--- Begin report of {book_path} ---")
print(f"{words_count} words found in the document")
print()
for c in characters_by_frequency_desc:
print(f"The '{(c['character'])}' character was found {(c['num'])} times")
print("--- End report ---")
def get_book_text(path):
with open(path) as f:
return f.read()
def get_words_count(fulltext):
words = fulltext.split()
return len(words)
def get_characters_count(fulltext):
count = {}
lowered_text = fulltext.lower()
characters = list(lowered_text)
for c in characters:
if c in count :
count[c] += 1
else:
count[c] = 1
return count
def sort_on(count):
return count["num"]
def get_characters_by_frequency_desc (count):
count_in_order = []
for c in count :
if c.isalpha() == True:
count_in_order.append({"character" : c , "num" : count[c]})
count_in_order.sort(reverse=True, key=sort_on)
return count_in_order
def characters_report(count):
for c in count:
return (f"The {c} character was found {characters_count[c]} times")
main ()