-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
48 lines (33 loc) · 1.06 KB
/
main.py
File metadata and controls
48 lines (33 loc) · 1.06 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
def print_number_of_words(contents):
words = contents.split()
print(f"{len(words)} words found in the document")
def count_characters(contents):
results = {}
converted_text = contents.lower()
for char in converted_text:
if char in results:
results[char] += 1
else:
results[char] = 1
return results
def sort_on(dict):
return dict["count"]
def get_book_text(path):
with open(path) as f:
return f.read()
def main():
book_path = "books/frankenstein.txt"
file_contents = get_book_text(book_path)
print(f"--- Begin report of {book_path} ---")
print_number_of_words(file_contents)
print()
results = count_characters(file_contents)
results_list = []
for val in results:
results_list.append({"char": val, "count": results[val]})
results_list.sort(reverse=True, key=sort_on)
for val in results_list:
if val["char"].isalpha():
print(f"The '{val["char"]}' character was found {val["count"]} times")
print("--- End report ---")
main()