#C15. Group Strings by Their Length
Group Strings by Their Length
Group Strings by Their Length
Given a list of strings, your task is to group the strings by their lengths. For each distinct length, collect all strings of that length, sort them in lexicographical order, and output a dictionary where keys are the lengths and values are the sorted lists of strings. The keys in the dictionary must appear in ascending order.
You are required to read the input from standard input (stdin) and output the result to standard output (stdout). If the input list is empty, output an empty dictionary {}
.
Note: In the output, use Python dictionary format. For example, the output for an input of ["hello", "hi", "world", "python", "java", "code"]
should be: {2: ['hi'], 4: ['code', 'java'], 5: ['hello', 'world'], 6: ['python']}
.
inputFormat
The input begins with an integer n representing the number of strings. The next n lines each contain one string.
Example:
6 hello hi world python java code
outputFormat
Print the resulting dictionary grouping strings by their lengths. The dictionary should be printed in the Python dictionary format with keys in ascending order.
Example:
{2: ['hi'], 4: ['code', 'java'], 5: ['hello', 'world'], 6: ['python']}## sample
6
hello
hi
world
python
java
code
{2: ['hi'], 4: ['code', 'java'], 5: ['hello', 'world'], 6: ['python']}
</p>