-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
57 lines (46 loc) · 1.44 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
def main():
BOOK_PATH = "books/frankenstein.txt"
text = get_book_text(BOOK_PATH)
words = text.split()
char_count = get_chars_dict(words)
sorted_chars = get_sorted_chars(char_count)
report_char_count(sorted_chars, BOOK_PATH)
def get_sorted_chars(char_count):
"""
Takes in a dict of char -> count pairs and
Returns an array of char objects sorted by count descending
"""
chars = [] # array of char objects containing char and its count
for c in char_count:
char = {
"char": c,
"count": char_count[c]
}
chars.append(char)
# sort the array of chars based on count
chars.sort(reverse=True, key=lambda char: char["count"])
return chars
def report_char_count(chars, fname):
"""
Takes in an array of char objects and
Prints out a report of char frequencies
Returns None
"""
print(f"---------- BEGIN REPORT OF {fname} ----------")
for char in chars:
if char["char"].isalpha():
print(f"The '{char['char']}' character was found {char['count']} times")
continue
print(f"---------- END REPORT OF {fname} ----------")
return
def get_chars_dict(words):
char_count = {}
for word in words:
lowered = word.lower()
for c in lowered:
char_count[c] = char_count.get(c, 0) + 1
return char_count
def get_book_text(path):
with open(path) as f:
return f.read()
main()