Skip to content

Instantly share code, notes, and snippets.

@chetgray
Last active February 4, 2022 18:54
Show Gist options
  • Save chetgray/d0301185ac51b4e39c61c787610d57fa to your computer and use it in GitHub Desktop.
Save chetgray/d0301185ac51b4e39c61c787610d57fa to your computer and use it in GitHub Desktop.

Revisions

  1. chetgray revised this gist Feb 4, 2022. No changes.
  2. chetgray renamed this gist Feb 4, 2022. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. chetgray created this gist Feb 4, 2022.
    12 changes: 12 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    # [The LibraryThing programming quiz!](https://thingology.librarything.com/2011/07/the-librarything-programming-test/)

    [...]

    Input is a string—a paragraph of text. One of the paragraphs above would be fine.

    Output is a report listing how many words there are with X letters, like:

    10 words with 1 letter
    20 words with 2 letters
    7 words with 3 letters
    15 words with 4 letters, etc.
    13 changes: 13 additions & 0 deletions word_counts.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    from collections import defaultdict
    import re

    def print_word_counts(text: str="") -> None:
    words = re.split("[\W_]+", text)
    length_counts = defaultdict(int)
    for word in words:
    length_counts[len(word)] += 1
    for length in sorted(length_counts.keys()):
    print(
    f"{length_counts[length]} word{'s' if length_counts[length] != 1 else ''}"
    f"with {length} letter{'s' if length != 1 else ''}"
    )