Skip to content

Instantly share code, notes, and snippets.

@gerep
Created July 2, 2023 13:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gerep/f193da1f69eecd13de9c37a9c487f806 to your computer and use it in GitHub Desktop.
Save gerep/f193da1f69eecd13de9c37a9c487f806 to your computer and use it in GitHub Desktop.
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