-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
43 lines (34 loc) · 1.06 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
def main():
book_path = "books/frankenstein.txt"
text = get_book_text(book_path)
num_words = get_num_words(text)
chars_dict = get_chars_dict(text)
print(chars_dict)
print_pretty_dict(chars_dict, book_path)
def get_num_words(text):
words = text.split()
return len(words)
def get_chars_dict(text):
chars = {}
for c in text:
lowered = c.lower()
if lowered in chars:
chars[lowered] += 1
else:
chars[lowered] = 1
return chars
def get_book_text(path):
with open(path) as f:
return f.read()
def alpha_only(text):
return text if text.key().isalpha() else None
def print_pretty_dict(text, path):
spacer = "---"
sorted_dict = dict(sorted(text.items(), key=lambda x:x[1], reverse=True))
# filtered_dict = {filter(alpha_only, sorted_dict.items())}
print(f"{spacer} Begin report of {path} {spacer}")
for k, v in sorted_dict.items():
if k.isalpha():
print(f"The {k} character was found {v} times")
print(f"{spacer} End report {spacer}")
main()