-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
37 lines (30 loc) · 946 Bytes
/
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
def get_num_words(text):
return len(text.split())
def get_char_counts(text):
chars = {}
for char in text.lower():
if(char in chars):
chars[char] += 1
else:
chars[char] = 1
return chars
def char_sort(dict):
return dict["count"]
def get_char_count_list(text):
char_counts = []
for key, value in get_char_counts(text).items():
if key.isalpha():
char_counts.append({"char": key, "count": value})
char_counts.sort(reverse = True, key=char_sort)
return char_counts
def get_book_text(path):
with open(path) as f:
return f.read()
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")
for char in get_char_count_list(text):
print(f"The '{char["char"]}' character was found {char["count"]} times")
main()