-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
29 lines (24 loc) · 869 Bytes
/
main.py
File metadata and controls
29 lines (24 loc) · 869 Bytes
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
def sort_on(dict):
return dict["number"]
def main():
with open('books/Frankenstein.txt', 'r') as f:
data = f.read()
data = data.lower()
words = data.split()
chars = [c for c in data]
countsForReport = []
for char in set(chars):
if char.isalpha():
counts = {}
matches = [x for x in chars if x == char]
counts["char"] = char
counts["number"] = len(matches)
countsForReport.append(counts)
countsForReport.sort(reverse=True, key = sort_on)
message = f"--- Begin report of books/frankenstein.txt --- \n{len(words)} words found in the document \n"
for i in countsForReport:
message += f"\nThe '{i["char"]}' character was found {i["number"]} times"
message += "\n--- End report ---"
print(message)
if __name__ == "__main__":
main()