-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
62 lines (38 loc) · 1.27 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
def read_book(file_path):
with open(file_path) as f:
file_data = f.read()
return file_data
def count_words(file_data):
words = file_data.split()
word_count = len(words)
return word_count
def count_characters(file_data):
char_count = {}
file_data = file_data.lower()
for char in file_data:
if char in char_count:
char_count[char] += 1
else:
char_count[char] = 1
return char_count
def sort_dict(dict_to_sort):
as_list = []
for key in dict_to_sort:
if key.isalpha():
as_list.append({"letter": key, "count": dict_to_sort[key]})
as_list.sort(reverse=True, key=lambda x: x["count"])
return as_list
def create_report(word_count, char_count):
print("--- Begin report of books/frankenstein.txt ---\n")
print(f"{word_count} words found in the document\n")
ordered_dict_as_list = sort_dict(char_count)
for item in ordered_dict_as_list:
print(f"The '{item["letter"]}' character was found {item["count"]} times")
return 0
def main():
file_data = read_book("books/frankenstein.txt")
word_count = count_words(file_data)
char_count = count_characters(file_data)
create_report(word_count, char_count)
return 0
main()