#K33387. Consolidate Data Records
Consolidate Data Records
Consolidate Data Records
You are given a list of records. Each record consists of a name
(a string) and a value
(an integer). Your task is to consolidate these records by summing the values for records with the same name
.
If the input indicates a null list, the result should be an empty dictionary.
Formally, if the input contains records \(\{(name_i, value_i)\}_{i=1}^{n}\), you should output a dictionary \(D\) where
\[
D[name] = \sum_{i: name_i = name} value_i
\]
for each unique name
in the input.
inputFormat
The input is read from standard input (stdin) in the following format:
- The first line contains an integer
n
. - If
n == -1
, it indicates that the input is null and should be treated as an empty list. - If
n >= 0
, then the nextn
lines each contain a record. Each record consists of a string and an integer separated by a space, representingname
andvalue
respectively.
outputFormat
Output to standard output (stdout) the consolidated dictionary in JSON format. The dictionary should map each unique name to the sum of its values. If there is no record (i.e. the input is null or empty), output an empty dictionary {}
.
Note: The order of keys in the output does not matter.
## sample3
apple 10
banana 5
apple 3
{"apple": 13, "banana": 5}