-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
48 lines (38 loc) · 1.11 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
def main ():
book_path = "books/frankenstein.txt"
text = get_book_text(book_path)
word_count = get_word_count(text)
char_count = get_character_count(text)
report = get_report(char_count)
print(f"--- Begin report of {book_path} ---")
print(f"{word_count} words found in the document")
print("\r")
for i in range(len(report)):
print(f"The '{report[i]['char']}' character was found {report[i]['count']} times")
print("\r")
print("--- End report ---")
def get_book_text(path):
with open(path) as f:
return f.read()
def get_word_count(text):
split = text.split()
return len(split)
def get_character_count(text):
dict = {}
for char in text:
char = char.lower()
if char in dict:
dict[char] = dict[char] + 1
else:
dict[char] = 1
return dict
def sort_on(dict):
return dict['count']
def get_report(dict):
list=[]
for item in dict:
if item.isalpha():
list.append({"char": item, "count": dict[item]})
list.sort(reverse=True, key=sort_on)
return list
main()