-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
46 lines (36 loc) · 1.26 KB
/
main.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
42
43
44
45
46
def main():
book_path = "books/frankenstein.txt"
text = get_book_text(book_path)
word_count = count_words(text)
char_count = count_characters(text)
sorted_characters = sort_char_count(char_count)
print_report(book_path, word_count, sorted_characters)
def get_book_text(path):
with open(path) as f:
return f.read()
def count_words(text):
words = text.split()
return len(words)
def count_characters(text):
text = text.lower()
char_count = {}
for char in text:
if char.isalpha():
if char in char_count:
char_count[char] += 1
else:
char_count[char] = 1
return char_count
def sort_char_count(char_count):
sorted_char_count = [{'char': k, 'count': v} for k, v in char_count.items()]
sorted_char_count.sort(key=lambda x: x['count'], reverse=True)
return sorted_char_count
def print_report(book_path, word_count, sorted_characters):
print(f"--- Begin report of {book_path} ---")
print(f"{word_count} words found in the document\n")
for entry in sorted_characters:
char = entry['char']
count = entry['count']
print(f"The '{char}' character was found {count} times")
print("--- End report ---")
main()