-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·54 lines (40 loc) · 1.33 KB
/
Copy pathmain.py
File metadata and controls
executable file
·54 lines (40 loc) · 1.33 KB
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
#!/usr/bin/env python
def main():
book_path = "books/frankenstein.txt"
book_contents = get_book_contents(book_path)
word_count = get_word_count(book_contents)
char_count_d = get_char_count_dict(book_contents)
char_list = get_sorted_char_list(char_count_d)
print_report(book_path, word_count, char_list)
def print_report(book_path, word_count, char_list):
print(f"--- Begin report of {book_path} ---")
print(f"{word_count} words found in the document")
print()
for entry in char_list:
print(f"The '{entry[1]}' character was found {entry[0]} times")
print("--- End report ---")
def get_sorted_char_list(char_dict):
char_list = []
for k, v in char_dict.items():
if k.isalpha():
char_entry = (v, k)
char_list.append(char_entry)
char_list.sort(reverse=True)
return char_list
def get_word_count(book_contents):
words = book_contents.split()
return len(words)
def get_char_count_dict(book_contents):
lower_contents = book_contents.lower()
char_count = {}
for c in lower_contents:
if c in char_count:
char_count[c] += 1
else:
char_count[c] = 1
return char_count
def get_book_contents(book_path):
with open(book_path) as f:
file_contents = f.read()
return file_contents
main()