#K54127. Compute Sum of Nested Dictionary Values
Compute Sum of Nested Dictionary Values
Compute Sum of Nested Dictionary Values
You are given a structure representing a nested dictionary where each key maps to a list of numbers. Your task is to compute the sum of the numbers in each list and output the result by preserving the original keys and their corresponding sums.
The input is provided via standard input (stdin) and the output should be printed to standard output (stdout). Use the format described below.
In particular, if the sums are whole numbers, they should be printed as integers; if they are real numbers with decimal parts, print them as floating point numbers.
You may find the following mathematical representation useful:
For a dictionary \(D\) with keys \(k_1, k_2, \dots, k_m\) and values \(D[k_i] = [a_{i1}, a_{i2}, \dots, a_{in_i}]\), the output dictionary \(S\) is defined as:
[ S[k_i] = \sum_{j=1}^{n_i} a_{ij} \quad \text{for every key } k_i. ]
inputFormat
The first line contains an integer \(n\) representing the number of key-value pairs. Each of the next \(n\) lines represents a key and its corresponding list of numbers. Each such line has the following format:
key count num1 num2 ... num{count}
Here, key
is a string (without spaces), count
is the number of numbers in the list, followed by exactly count
space-separated numbers (which may be integers or floating point numbers).
outputFormat
For each key in the order of input, output a line containing the key and the sum of its list of numbers, separated by a space. If the sum is an integer, output it as an integer; if it is a floating point number, output the decimal value.
## sample2
a 4 1 2 3 4
b 4 5 6 7 8
a 10
b 26
</p>