#C14881. Group People by City
Group People by City
Group People by City
You are given a list of people. Each person is described by a name, an age, and optionally a city. Your task is to group the people by their city. If the city
field is missing for any person, place that person in a group with the key Unknown
.
The output should be a JSON object where each key is a city name and the corresponding value is an array of the names of the people from that city.
Note: In the output, use the exact key Unknown
for entries without a provided city.
The algorithm can be expressed mathematically as follows. Let \(P\) be the set of people, and for each person \(p \in P\) define the city as
[ city(p) = \begin{cases} p.city, & \text{if } p.city \text{ is provided},\ \text{Unknown}, & \text{otherwise.} \end{cases} ]
Then, we want to compute the mapping:
[ G = { c : [p.name \mid p \in P,\ city(p)=c] \mid c \in \text{all cities}}. ]
</p>inputFormat
The input is read from standard input (stdin) in the following format:
- The first line contains an integer
n
representing the number of people. - Each of the next
n
lines contains information for one person in the following format:
name age [city]
Here,name
is a string,age
is a number, andcity
(without spaces) is optional. Ifcity
is missing, treat it asUnknown
.
outputFormat
Output a JSON object to standard output (stdout) where the keys are the city names and the values are arrays of names of the people belonging to that city.
## sample3
Alice 30 New_York
Bob 25 San_Francisco
Charlie 35 New_York
{"New_York": ["Alice", "Charlie"], "San_Francisco": ["Bob"]}