#K46347. Filter Books Problem
Filter Books Problem
Filter Books Problem
You are given an inventory of books. Each book has an id, title, author, genre, and pages field. Your task is to filter out the books based on two criteria:
- The book's genre must be one of the specified genres (if the list of genres is empty, then no genre filtering is applied).
- The book's page count must be less than or equal to a given maximum. A special value of
-1
indicates there is no page limit.
After applying the filters, output the titles of the remaining books sorted in alphabetical order.
The filtering condition can be mathematically represented as:
$$ \text{Include book if } \Big[(\text{genres list is empty}) \;\lor\; (\text{book.genre} \in \text{genres})\Big] \quad \land \quad \Big[(\text{max\_pages} = -1) \;\lor\; (\text{book.pages} \leq \text{max\_pages})\Big] $$inputFormat
The input is provided via stdin and has the following format:
N id1 title1 author1 genre1 pages1 id2 title2 author2 genre2 pages2 ... (N lines for N books) K [genre1 genre2 ... genreK] (if K > 0, otherwise this line is omitted) max_pages
N is the number of books. Each of the next N lines describes a book. The next line contains an integer K representing the number of genres to filter by. If K is greater than 0, the following line contains K genre names separated by spaces. The last line contains an integer max_pages. A value of -1
for max_pages signifies there is no limit on the number of pages.
outputFormat
The output should be written to stdout and consist of the filtered book titles, each on a separate line, sorted in alphabetical order. If no book matches the filter criteria, no output should be produced.
## sample3
1 BookA AuthorA Fantasy 300
2 BookB AuthorB Sci-Fi 250
3 BookC AuthorC Fantasy 400
1
Fantasy
-1
BookA
BookC
</p>