Sort words from shortest to longest.
What to build
A one-line sort with a key.
Requirements
- Read a line of words.
- Print them from shortest to longest, separated by spaces.
- Words of the same length stay in their original order.
Input
One line of words separated by spaces.
Output
One line.
Constraints
There are from 1 to 1000 words.
Example
Input:
pear fig banana kiwi
Output:
fig pear kiwi banana
Why it works
key=len compares by length, and Python's sort is stable, so ties stay as they were.
Examples
Example 1 · Input
pear fig banana kiwi
Expected output
fig pear kiwi banana
Example 2 · Input
a
Expected output
a
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Use sorted with key=len.
Hint 2
Equal lengths keep their original order automatically.
Solution
Show one correct solution (try it yourself first)
words = input().split()
print(" ".join(sorted(words, key=len)))