#C9679. Grouping Elements by Function

    ID: 53798 Type: Default 1000ms 256MiB

Grouping Elements by Function

Grouping Elements by Function

You are given a list of elements along with a function indicator. Your task is to group the elements into sublists based on the output of the specified function. The available function indicators are:

  • mod2: For integer inputs, group the numbers by their remainder when divided by 2, i.e. \(x \bmod 2\).
  • length: For string inputs, group the strings by their length, i.e. \(\mathrm{len}(s)\).
  • identity: For integer inputs, group the numbers by their own value.

The output should list each distinct key (in ascending order) followed by the grouped elements corresponding to that key. Each line of output must follow the format:

[ key: element_{1}\ element_{2}\ \ldots ]

Note: Input is read from stdin and output must be produced to stdout.

inputFormat

The input (from stdin) consists of:

  1. A line containing a function indicator, which can be one of "mod2", "length", or "identity".
  2. A line with an integer n, representing the number of elements.
  3. A line with n space-separated elements. For the indicators "mod2" and "identity", these elements are integers; for "length", they are strings.

outputFormat

For each distinct grouping key, output a line in the following format:

key: element1 element2 ...

The groups must be printed in ascending order of their keys.

## sample
mod2
6
1 2 3 4 5 6
0: 2 4 6

1: 1 3 5

</p>