Created
July 2, 2023 13:19
-
-
Save gerep/f193da1f69eecd13de9c37a9c487f806 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import Counter | |
def main(): | |
with open("books/frankenstein.txt", "r") as file: | |
contents = file.read() | |
print("--- Begin report of books/frankenstein.txt ---") | |
print(f"{count_words(contents)} word found in the document\n") | |
letters = count_letters(contents) | |
sorted_letters = sorted(letters.items(), key=lambda x: x[1], reverse=True) | |
for k, v in sorted_letters: | |
print(f"The '{k}' character was found {v} times") | |
print("--- End report ---") | |
def count_words(content): | |
return len(content.split()) | |
def count_letters(content): | |
content = content.lower() | |
return Counter(c for c in content if c.isalpha()) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment