#C13722. Book Summary Statistics

    ID: 43292 Type: Default 1000ms 256MiB

Book Summary Statistics

Book Summary Statistics

Given a CSV input representing a collection of books, your task is to generate a summary report containing:

  • Year Summary: The number of books published in each year.
  • Genre Summary: The number of books for each genre.

The CSV input will be provided via standard input (stdin) and will include a header row "Title,Author,Year,Genre" followed by one or more lines of book records. Your program should output a JSON object with two keys, year_summary and genre_summary.

For example, if the input CSV is:

Title,Author,Year,Genre
1984,George Orwell,1949,Dystopian
To Kill a Mockingbird,Harper Lee,1960,Classic
The Great Gatsby,F. Scott Fitzgerald,1925,Classic
Brave New World,Aldous Huxley,1932,Dystopian

Then the output should be a JSON object similar to:

{"genre_summary":{"Classic":2,"Dystopian":2},"year_summary":{"1925":1,"1932":1,"1949":1,"1960":1}}

Note: The counting logic can be described mathematically as follows:

For each record, let \(y\) be the year and \(g\) be the genre. Then, we compute:

\(year\_summary[y] = year\_summary[y] + 1\) and \(genre\_summary[g] = genre\_summary[g] + 1\).

Your solution should read the CSV data from standard input and print the JSON summary to standard output.

inputFormat

The input will be provided via stdin and is a CSV formatted text consisting of multiple lines. The first line is the header:

Title,Author,Year,Genre

Each subsequent line contains a book record with the following fields separated by commas:

  • Title
  • Author
  • Year
  • Genre

outputFormat

Output a single JSON object (to stdout) with exactly two keys:

  • year_summary: an object where each key is a year (as a string) and the value is the count of books published in that year.
  • genre_summary: an object where each key is a genre and the value is the count of books in that genre.

The JSON must be valid and use double quotes for keys and string values.

## sample
Title,Author,Year,Genre
1984,George Orwell,1949,Dystopian
To Kill a Mockingbird,Harper Lee,1960,Classic
The Great Gatsby,F. Scott Fitzgerald,1925,Classic
Brave New World,Aldous Huxley,1932,Dystopian
{"genre_summary":{"Classic":2,"Dystopian":2},"year_summary":{"1925":1,"1932":1,"1949":1,"1960":1}}