-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
49 lines (39 loc) · 1.24 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():
book_path = "books/frankenstein.txt"
text = get_book_text(book_path)
num_words = get_num_words(text)
#print(f"{num_words} words found in the document")
char_dict = get_char_dictionary(text)
sort_dict = sort_char_dictionary(char_dict)
print_report(book_path, sort_dict)
def print_report(path, sort_dict):
print(f"---Begin report of {path} ---")
for i in range(0,len(sort_dict)):
if sort_dict[i]['char'].isalpha():
print(f"The '{sort_dict[i]['char']}' character was found {sort_dict[i]['num']} times")
print(f"--- End report ---")
def get_num_words(text):
words = text.split()
return len(words)
def get_book_text(path):
with open(path) as f:
return f.read()
def sort_on(dict):
return dict["num"]
def get_char_dictionary(text):
char_counts = {}
for c in text:
c_lower = c.lower()
if c_lower in char_counts:
char_counts[c_lower] += 1
else:
char_counts[c_lower] = 1
return char_counts
def sort_char_dictionary(dict):
char_list = []
count = 0
for d in dict:
char_list.append({"char": d, "num": dict[d]})
char_list.sort(reverse=True, key=sort_on)
return char_list
main()