-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
49 lines (39 loc) · 1.47 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
def main():
path = "books/frankenstein.txt"
words = get_text_of_book(path)
num_of_words = num_of_words_in_book(words)
character_dictionary = num_of_times_character_appears(words)
book_report_list = character_dictionary_in_sorted_list(character_dictionary)
#Book Report
print(f"--- Begin report of {path} ---")
print(f"{num_of_words} words found in the document")
print("")
for character in book_report_list:
alpha = character["character"].isalpha()
if alpha:
print(f"The '{character['character']}' character was found {character['num']} times")
print("--- End report ---")
def sort_on(dict):
return dict["num"]
def character_dictionary_in_sorted_list(character_dictionary):
sorted_list_of_characters = []
for character in character_dictionary:
sorted_list_of_characters.append({"character": character, "num": character_dictionary[character]})
sorted_list_of_characters.sort(reverse=True, key=sort_on)
return sorted_list_of_characters
def num_of_words_in_book(book):
words = book.split()
return len(words)
def get_text_of_book(path):
with open(path) as f:
return f.read()
def num_of_times_character_appears(text):
characters = {}
for string in text:
lowered_string = string.lower()
if lowered_string in characters:
characters[lowered_string] += 1
else:
characters[lowered_string] = 1
return characters
main()