Skip to content

Instantly share code, notes, and snippets.

@gerep
Created July 2, 2023 13:19

Revisions

  1. Gerep created this gist Jul 2, 2023.
    28 changes: 28 additions & 0 deletions bootdev-localenv.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    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()