#C14687. Categorize Books by Decade
Categorize Books by Decade
Categorize Books by Decade
In this problem, you are given a JSON string representing a list of books. Each book is a JSON object with the keys "title", "author", "year", and "rating". Your task is to parse this JSON and categorize the books according to their publication decade. For each decade, sort the list of books in descending order by their rating.
The decade is computed by the formula: $$\text{decade} = \lfloor \text{year} / 10 \rfloor \times 10$$. The output should be a JSON object where each key is a decade (e.g., "1980s", "1990s", etc.) and each value is the list of books published in that decade.
inputFormat
The input consists of a single JSON string read from standard input. This string represents a list of books. Each book is described by a JSON object with the following fields:
- title: a string representing the title of the book
- author: a string representing the author
- year: an integer representing the year of publication
- rating: a float representing the rating of the book
outputFormat
Print a JSON string to standard output representing a dictionary. Each key in the dictionary is a decade (e.g., "1980s") and its corresponding value is a list of book objects for that decade. The books in each list must be sorted in descending order by their rating. If there are no books, output an empty JSON object {}
.## sample
[
{"title": "Book A", "author": "Author A", "year": 1985, "rating": 4.5},
{"title": "Book B", "author": "Author B", "year": 1992, "rating": 3.8}
]
{"1980s": [{"title": "Book A", "author": "Author A", "year": 1985, "rating": 4.5}], "1990s": [{"title": "Book B", "author": "Author B", "year": 1992, "rating": 3.8}]}