Last active
February 4, 2022 18:54
-
-
Save chetgray/d0301185ac51b4e39c61c787610d57fa to your computer and use it in GitHub Desktop.
Revisions
-
chetgray revised this gist
Feb 4, 2022 . No changes.There are no files selected for viewing
-
chetgray renamed this gist
Feb 4, 2022 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
chetgray created this gist
Feb 4, 2022 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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. This file contains hidden or 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 charactersOriginal 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 ''}" )