-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
28 lines (22 loc) · 788 Bytes
/
main.py
File metadata and controls
28 lines (22 loc) · 788 Bytes
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
def main():
with open("books/frankenstein.txt") as book:
book_contents = book.read()
print_report(book_contents)
def count_words(book):
return len(book.split())
def character_counts(book):
total_counts = {}
for char in book.lower():
if(char in total_counts):
total_counts[char] += 1
else:
total_counts[char] = 0
return total_counts
def print_report(book):
print("--- Begin report of books/frankenstein.txt ---")
print(f"{count_words(book)} words found in the document.")
counts = character_counts(book)
sorted_dict = dict(sorted(counts.items(), key=lambda item: item[1]))
for count in sorted_dict.items():
print(f"The character {count[0]} occurred {count[1]} times.")
main()