#C13830. Group People by Age
Group People by Age
Group People by Age
You are given records of people, where each record contains a person's name and age. The input begins with an integer N which represents the number of records. Then N lines follow, where each line has two tokens: a name and an age separated by a space. If either the name or the age is missing, it is represented by the string NA
and that record should be ignored.
Your task is to group the people by their age. For each group, output the age, the count of people with that age, and the names of those people in the order they appear in the input. The groups must be output in ascending order of age.
Formally, if you denote the group for age a as a list of names, then for valid records the grouping function should satisfy:
$$\text{group}(a) = \{\text{names } \mid \text{ person.age} = a \}\,. $$Make sure your solution reads from stdin
and writes to stdout
.
inputFormat
The input is given via standard input. The first line contains an integer N, the number of people records. Each of the following N lines contains two tokens: a string (the person's name) and an integer (the person's age), separated by a space. If either the name or the age is missing, it will be represented by the string NA
and that record must be ignored.
outputFormat
First, output an integer K representing the number of distinct ages (groups). Then, for each group (in ascending order by age), output a line with the following format:
age count name1 name2 ... name_count
where age
is the age of the group, count
is the number of people with that age, and name1
, name2
, ... are the names of the people in the order they appeared in the input.## sample
5
Alice 30
Bob 25
Charlie 30
David 25
Eve 35
3
25 2 Bob David
30 2 Alice Charlie
35 1 Eve
</p>