-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
58 lines (41 loc) · 1.23 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
54
55
56
57
58
def main():
book_path = "books/frankenstein.txt"
# Print header
print(f"--- Begin report of {book_path} ---")
# Get and print word count
text = get_book_text(book_path)
num_words = get_num_words(text)
print(f"{num_words} words found in the document\n")
# Get character count and convert to sorted list
char_counts = count_characters(text)
char_list = convert_to_list(char_counts)
# Print each character count
for char_dict in char_list:
print(f"The '{char_dict['char']}' was found {char_dict['num']}'")
print("--- End report ---")
def get_book_text(path):
with open(path) as f:
return f.read()
def get_num_words(text):
return len(text.split())
def count_characters(text):
characters = {}
lower_text = text.lower()
for x in lower_text:
if x in characters:
characters[x] += 1
else:
characters[x] = 1
return characters
def sort_on(dict):
return dict["num"]
def convert_to_list(characters):
alpha = [
{"char": key, "num": value}
for key, value in characters.items()
if key.isalpha()
]
alpha.sort(reverse=True, key=sort_on)
return alpha
if __name__ == "__main__":
main()