#K7946. Calculate Genre Totals

    ID: 35313 Type: Default 1000ms 256MiB

Calculate Genre Totals

Calculate Genre Totals

You are given multiple datasets containing inventory records. Each record is a string formatted as ISBN GENRE COPIES, where ISBN is an identifier, GENRE is the book's genre, and COPIES is the number of copies available. Your task is to compute the total number of copies for each genre in every dataset.

For example, if a dataset contains the records 12345 Fiction 5 and 67890 Fiction 3, then the total for the genre Fiction is 8. Formally, if for a given dataset the records are given by

[ record = (\text{ISBN},,g,,c) ]

then for each genre ( g ) you should compute

[ \text{total}(g) = \sum_{record;with;genre;g} c ]

and output the results as a JSON object (dictionary) for that dataset. Your final output should be a JSON array of these dictionaries.

inputFormat

The input is provided via stdin with the following format:

  1. An integer (T) representing the number of datasets.
  2. For each dataset:
    • An integer (N) denoting the number of records in this dataset.
    • (N) lines follow, each line is a record in the format: ISBN GENRE COPIES (separated by spaces).

Example:

2 3 12345 Fiction 5 67890 Fiction 3 54321 Thriller 2 4 11223 Mystery 7 33445 Fiction 10 55667 Mystery 3 77889 SciFi 6

outputFormat

Output a JSON array (to stdout) where each element is a dictionary corresponding to a dataset. In each dictionary, the keys are the genres (strings) and the values are the total number of copies (integers) for that genre. There should be no extra spaces, and the JSON should be valid.

Example Output:

[{"Fiction":8,"Thriller":2},{"Mystery":10,"Fiction":10,"SciFi":6}]## sample

1
3
12345 Fiction 5
67890 Fiction 3
54321 Thriller 2
[{"Fiction":8,"Thriller":2}]