-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
32 lines (25 loc) · 903 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
def main():
with open("books/frankenstein.txt") as f:
file_contents = f.read()
words = file_contents.split()
print(f"{len(words)} words found in the document")
characterCount(file_contents)
def sort_on(dict):
return dict["num"]
def characterCount(file_contents):
# Initialize an empty dictionary
char_counts = {}
for char in file_contents:
char = char.lower()
if char.lower().isalpha():
if char in char_counts:
char_counts[char] += 1
else:
char_counts[char] = 1
listofdict = [{"letter": key, "num": val} for key, val in char_counts.items()]
listofdict.sort(reverse=True, key=sort_on)
for dictionary in listofdict:
letter = dictionary["letter"]
num = dictionary["num"]
print(f"The '{letter}' character was found {num} times")
main()