From ad5c97a0110035d39c6b46e293be8d3bd8d27eb6 Mon Sep 17 00:00:00 2001 From: rdarius Date: Sun, 19 Jan 2025 18:11:55 +0200 Subject: [PATCH] a description of your changes here --- .gitignore | 1 + main.py | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 .gitignore create mode 100644 main.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1503dd9 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +books/ \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..8ce665c --- /dev/null +++ b/main.py @@ -0,0 +1,23 @@ +chars = {} +filename = 'books/frankenstein.txt' + +with open(filename) as f: + print(f"--- Begin report of {filename} ---") + file_contents = f.read() + + words = len(file_contents.split()) + print(f"{words} words found in the document\n") + + for char in file_contents: + lower = char.lower() + if 'a' <= lower and lower >= '<': + if lower not in chars: + chars[lower] = 0 + chars[lower] += 1 + + sorted_dict = dict(sorted(chars.items(), key=lambda item: item[1], reverse=True)) + + for char in sorted_dict: + print(f"The '{char}' character was found {chars[char]} times") + + print('--- End report ---')