Words by Length

When you have many words, it is often useful to group short words with short ones and long words with long ones. Write a function group_by_length that returns a dictionary where each key is a word length as a string, such as "2", and each value is a list of words with that length. Keep the words inside each group in the same order as they appeared in the input list.

Примеры

Ввод
["a", "bb", "cc", "ddd"]
Вывод
{"1": ["a"], "2": ["bb", "cc"], "3": ["ddd"]}
Ввод
[]
Вывод
{}
Ввод
["hi", "to", "go"]
Вывод
{"2": ["hi", "to", "go"]}

Нужно реализовать

def group_by_length(words: list[str]) -> dict[str, list[str]]:

Теги

stringsdicthashmap
PostgreSQLv16

Taispeánfar toradh do chóid anseo

Visible tests
1
Input
["a","bb","cc","ddd"]
Expected{"1":["a"],"2":["bb","cc"],"3":["ddd"]}
2
Input
[]
Expected{}
3
Input
["hi","to","go"]
Expected{"2":["hi","to","go"]}
4
Input
["x"]
Expected{"1":["x"]}
5
Input
["one","two","six","three"]
Expected{"3":["one","two","six"],"5":["three"]}
6
Input
["ab","cde","f"]
Expected{"1":["f"],"2":["ab"],"3":["cde"]}
Focus radio
Paused · SomaFM · Fluid