Point OA 面试真题解析:Python Book Popularity Tracker(书店库存与推荐)

19次阅读
没有评论

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:

  • Book class with attributes:
    • title (string): the book’s title
    • genre (string): the book’s genre
    • rating (float): the customer rating
  • LibraryManagement class with methods:
    • add_book(Book book): adds a new book to the inventory
    • get_highest_rated_books(int x): returns the x highest-rated books, sorted by rating (descending) and title (descending) for ties
    • recommend_top_book_by_genre(string genre): returns the highest-rated book in the specified genre, or None if 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) 时间返回结果。排序规则要注意:评分高的优先,评分相同时按标题的字典序做稳定比较。

正文完
 0