#C13749. Group Strings by Length
Group Strings by Length
Group Strings by Length
You are given a list of strings. Your task is to group these strings by their length. For each distinct length, output the list of strings that have that length. The order of strings within each group should be the same as the order in which they appeared in the input. The groups should be printed in increasing order of their length.
Input Format: The first line contains an integer \(n\) representing the number of strings. This is followed by \(n\) lines, each containing a non-empty string.
Output Format: Output a dictionary (in a Python-like literal format) where each key is the length of strings (an integer) and the corresponding value is a list of strings (each string enclosed in double quotes). The keys must be printed in increasing order. For example, if the grouped dictionary is {1: ["a"], 2: ["bb","dd"], 3: ["ccc","eee"]}
, you should output exactly that.
Use the following formula for the output formatting:
[ output = {key_1: ["s_{1,1}", "s_{1,2}", \ldots], key_2: ["s_{2,1}", "s_{2,2}", \ldots], \ldots}]
Make sure to read input from stdin
and send your output to stdout
.
inputFormat
The first line contains an integer \(n\) indicating the number of strings. The following \(n\) lines each contain one string.
outputFormat
Output the grouped dictionary in a Python-like literal format with keys in increasing order. For example:
{1: ["a"], 2: ["bb", "dd"], 3: ["ccc", "eee"]}## sample
5
a
bb
ccc
dd
eee
{1: ["a"], 2: ["bb", "dd"], 3: ["ccc", "eee"]}