Sol
Example heading with h2 size
Example heading with h3 size
Following is sample java code.
class Book:
def __init__(self, title, genre, rating):
self.title = title
self.genre = genre.strip() # Remove any leading or trailing whitespace
self.rating = rating
class LibraryManagement:
def __init__(self):
self.inventory = []
def add_book(self, book):
self.inventory.append(book)
def get_highest_rated_books(self, x):
if not self.inventory:
return []
# Sort books by rating in descending order and return the top x books
sorted_books = sorted(self.inventory, key=lambda b: b.rating, reverse=True)
return sorted_books[:min(x, len(sorted_books))]
def recommend_top_book_by_genre(self, genre):
genre = genre.strip().lower() # Normalize input genre
# Filter books by genre (case-insensitive) and get the one with the highest rating
genre_books = [book for book in self.inventory if book.genre.lower() == genre]
if genre_books:
top_book = max(genre_books, key=lambda b: b.rating)
return top_book
return None
# Sample input handling for custom testing
def main():
n = int(input().strip()) # Number of books
library = LibraryManagement()
for _ in range(n):
title, genre, rating = input().strip().split(', ')
rating = float(rating)
book = Book(title, genre, rating)
library.add_book(book)
x = int(input().strip()) # Number of books to recommend
genre = input().strip() # Genre to recommend from
highest_rated_books = library.get_highest_rated_books(x)
if highest_rated_books:
print("Highest Rated Books:")
for book in highest_rated_books:
print(f"{book.title}, Rating: {book.rating}")
else:
print("No books in the inventory.")
top_genre_book = library.recommend_top_book_by_genre(genre)