-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
29 lines (25 loc) · 925 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
def main():
with open("books/frankenstein.txt") as f:
file_contents = f.read()
word_count = len(file_contents.split())
lower_caps_file_contents = file_contents.lower()
letter_count = {}
for c in lower_caps_file_contents:
if c.isalpha():
if c in letter_count:
letter_count[c] += 1
else:
letter_count[c] = 1
letter_list = []
for d in letter_count:
letter_list.append((d, letter_count[d]))
letter_list.sort(reverse = True, key= tuple_get)
print("--- Begin report of books/frankenstein.txt ---")
print(f"{word_count} words found in the document")
print("")
for t in letter_list:
print(f"The {t[0]} character was found {t[1]} times")
print("--- End report ---")
def tuple_get(tuple):
return tuple[1]
main()