-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
31 lines (25 loc) · 854 Bytes
/
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
def sort_on(dict):
return dict['count']
def main():
file_path = "books/frankenstein.txt"
with open(file_path) as f:
char_counts = {}
char_count_list = []
file_contents = f.read().lower()
words = file_contents.split()
chars = "".join(file_contents)
for char in chars:
if char.isalpha():
if char in char_counts:
char_counts[char] += 1
else:
char_counts[char] = 1
for char, count in char_counts.items():
char_count_list.append({"char": char, "count": count})
char_count_list.sort(key=sort_on, reverse=True)
print(f"--- Begin report of {file_path} ---")
print(f"{len(words)} found in the document\n")
for entry in char_count_list:
print(f"The '{entry["char"]}' character was found {entry["count"]} times")
print("--- End report ---")
main()