#C12830. Book Records Processor
Book Records Processor
Book Records Processor
You are given a JSON string representing an array of book records. Each record contains the fields: title, author, genre, published_year, and copies_sold.
Your task is to process this JSON input as follows:
- Filter out books published before the year 2000.
- Group the remaining books by their genre.
- For each genre, compute the total number of books, identify the book with the highest number of copies sold, and calculate the average number of copies sold per book.
The average number of copies sold is computed using the formula: $$ \text{average} = \frac{\sum \text{copies\_sold}}{\text{total\_books}} $$
If the input JSON is not in the correct format, output exactly: Error: Incorrect JSON format.
inputFormat
The input consists of a single line read from standard input (stdin) containing a JSON string. This JSON string represents a list (array) of book records.
outputFormat
Output a JSON string to standard output (stdout) representing an object where each key is a genre. For each genre, the associated value is an object containing:
total_books
: the number of books in that genre,highest_selling_book
: the full record of the book with the highest copies sold,average_copies_sold
: the average copies sold (a floating point number) computed as $$ \frac{\sum \text{copies\_sold}}{\text{total\_books}} $$.
If the JSON format is incorrect, output only: Error: Incorrect JSON format.
[{"title": "Book1", "author": "Author1", "genre": "Fiction", "published_year": 2001, "copies_sold": 120}, {"title": "Book2", "author": "Author2", "genre": "Fiction", "published_year": 2005, "copies_sold": 150}, {"title": "Book3", "author": "Author3", "genre": "Non-Fiction", "published_year": 2010, "copies_sold": 200}, {"title": "Book4", "author": "Author4", "genre": "Fiction", "published_year": 1999, "copies_sold": 90}]
{"Fiction":{"total_books":2,"highest_selling_book":{"title":"Book2","author":"Author2","genre":"Fiction","published_year":2005,"copies_sold":150},"average_copies_sold":135.0},"Non-Fiction":{"total_books":1,"highest_selling_book":{"title":"Book3","author":"Author3","genre":"Non-Fiction","published_year":2010,"copies_sold":200},"average_copies_sold":200.0}}