-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
50 lines (35 loc) · 1.2 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
def main():
path_to_book = "./books/frankenstein.txt"
book_text = get_book_text(path_to_book)
word_count = count_words(book_text)
letter_count = count_letter(book_text)
letter_count_arr = []
for letter in letter_count:
letter_count_arr.append({"letter": letter, "num": letter_count[letter]})
letter_count_arr.sort(reverse=True, key=sort_on)
print(f"--- Begin report of {path_to_book} ---")
print(f"{word_count} words found in the document\n")
for letter in letter_count_arr:
print(f"The '{letter['letter']}' character was found {letter['num']} times")
print("--- End report ---")
def get_book_text(path_to_book):
with open(path_to_book) as f:
file_contents = f.read()
return file_contents
def count_words(text):
words = text.split()
text_length = len(words)
return text_length
def count_letter(text):
letter_count = {}
text_lowered = text.lower()
for char in text_lowered:
if char.isalpha():
if char not in letter_count:
letter_count[char] = 1
else:
letter_count[char] += 1
return letter_count
def sort_on(dict):
return dict["num"]
main()