-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
53 lines (36 loc) · 1.31 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
from typing import List, Dict
from collections import Counter
REPORT_HEADER = "--- Begin report of {} ---"
REPORT_FOOTER = "--- End report ---"
WORD_COUNT_MESSAGE = "{} words found in the document\n"
CHAR_COUNT_MESSAGE = "The '{}' character was found {} times"
def main():
book_path = "books/frankenstein.txt"
text = get_book_text(book_path)
words_amount = count_words(text)
chars_dict = count_chars(text)
result = chars_dict_to_sorted(chars_dict)
print(REPORT_HEADER.format(book_path))
print(WORD_COUNT_MESSAGE.format(words_amount))
for item in result:
if not item['char'].isalpha():
continue
print(CHAR_COUNT_MESSAGE.format(item['char'], item['num']))
print(REPORT_FOOTER)
def chars_dict_to_sorted(chars_dict: Dict[str, int]) -> List[Dict[str, int]]:
result = []
for char, value in chars_dict.items():
result.append({"char": char, "num": value})
result.sort(reverse=True, key=sort_chars)
return result
def sort_chars(d: Dict[str, int]) -> int:
return d['num']
def count_words(text: str) -> int:
return len(text.split())
def count_chars(text: str) -> Dict[str, int]:
return Counter(text.lower())
def get_book_text(path: str) -> str:
with open(path) as f:
return f.read()
if __name__ == "__main__":
main()