#C14738. Group Students by Grade
Group Students by Grade
Group Students by Grade
You are given a list of students. Each student is described by a name, a grade, and an age. Your task is to group the students by their grade and output the result as a JSON object.
In the output JSON, each key is a grade and its corresponding value is an array of student records (objects) in the order they appear in the input. Each student record contains the keys name
, grade
, and age
.
inputFormat
The input is read from stdin and has the following format:
n
name1 grade1 age1
name2 grade2 age2
...
nameN gradeN ageN
where the first line contains an integer n (n ≥ 0) representing the number of students. Each of the following n lines contains a student's details separated by spaces. Note that name
is a string (without spaces), grade
is a single capital letter, and age
is an integer.
outputFormat
Print to stdout a JSON object (as a string) that groups the students by their grade. The keys must be in the order of their first appearance in the input. The JSON object should match the format shown in the examples.## sample
3
Alice A 17
Bob B 18
David C 17
{"A": [{"name": "Alice", "grade": "A", "age": 17}], "B": [{"name": "Bob", "grade": "B", "age": 18}], "C": [{"name": "David", "grade": "C", "age": 17}]}