-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
52 lines (38 loc) · 1.21 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
47
48
49
50
51
52
def get_num_words(text):
words = text.split()
return len(words)
def get_book_text(path):
with open(path) as f:
return f.read()
def get_num_chars(text):
char_dict = {}
for word in text:
lowered_word = word.lower()
if lowered_word in char_dict:
char_dict[lowered_word] +=1
else:
char_dict[lowered_word] = 1
return char_dict
def sort_on(dict):
return dict['count']
def to_list(char_dict):
ls_char = []
for char in char_dict:
if char.isalpha():
ls_char.append({'character': char, 'count': char_dict[char]})
ls_char.sort(reverse=True, key=sort_on)
return ls_char
def main():
book_path = "books/frankenstein.txt"
text = get_book_text(book_path)
num_words = get_num_words(text)
chars_num = get_num_chars(text)
ls_chars_num = to_list(chars_num)
print("--- Begin report of books/frankenstein.txt ---")
print(f"{num_words} words found in the document")
for i in range(len(ls_chars_num)):
character = ls_chars_num[i]['character']
times = ls_chars_num[i]['count']
print(f"The {character} character was found {times} times")
print("--- End report ---")
main()