Group Words by Length
beginnerUp next
Functions
Package code into reusable, named blocks with parameters and return values.
Next lessonUp next
Package code into reusable, named blocks with parameters and return values.
Next lessonOutput will appear here...
The starter code reads a line of words into a list. Group the words by length and print one line per length, from the shortest length to the longest, in the format length: words. Within a line, keep the words in their original order and separate them with spaces. Repeated words are kept.
For the input cat dog bird fish ant, a correct solution prints:
3: cat dog ant
4: bird fish
Tip
Use a dictionary where each key is a length and each value is a list of words. Loop over sorted(groups) to print the lengths in order.
No comments yet. Start the discussion.