-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
43 lines (29 loc) · 1.1 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
def sort_on(dict):
return dict["num"]
def main():
with open('./books/frankenstein.txt') as f:
file_contents = f.read()
d_count_letters = {}
list_of_dictionaries = []
file_name = f.name
for letter in file_contents:
lower_letter = letter.lower()
if lower_letter.isalpha():
if lower_letter not in d_count_letters:
d_count_letters[lower_letter] = 1
else:
d_count_letters[lower_letter] += 1
for d in d_count_letters:
list_of_dictionaries.append({
"letter":d,
"num":d_count_letters[d]
})
list_of_dictionaries.sort(reverse=True,key=sort_on)
print(f"--- Begin report of books/{file_name}---")
print(f"{len(file_contents.split())} words found in the document")
print(" ")
print(" ")
for dictionary in list_of_dictionaries:
print(f"The '{dictionary["letter"]}' character was found {dictionary["num"]} times")
print('--- End report ---')
main()