Implement a Python application for managing a bookstore’s inventory. The application should allow adding books, finding highest-rated books, and recommending books by genre.
There are two classes:
Bookclass with attributes:title(string): the book’s titlegenre(string): the book’s genrerating(float): the customer rating
LibraryManagementclass with methods:add_book(Book book): adds a new book to the inventoryget_highest_rated_books(int x): returns thexhighest-rated books, sorted by rating (descending) and title (descending) for tiesrecommend_top_book_by_genre(string genre): returns the highest-rated book in the specified genre, orNoneif no books exist in that genre
The code for reading input, calling methods, and producing output is already provided.
Constraint: The number of recommended books is less than or equal to the number of books in the book list.
这道题要求你实现一个书店库存管理系统,核心是维护两类查询:一类是全局按评分排序取前 x 本书,另一类是按 genre 直接推荐该分类下评分最高的书。实现时通常会用一个全局堆 / 优先队列来支持 <code>get_highest_rated_books</code>,并额外用哈希表按 genre 记录当前最优书目,方便 <code>recommend_top_book_by_genre</code> 在 O(1) 或接近 O(1) 时间返回结果。排序规则要注意:评分高的优先,评分相同时按标题的字典序做稳定比较。