Unit 2 Session 1 (Click for link to problem statements)
Understand what the interviewer is asking for by using test cases and questions about the problem.
None
.Plan the solution with appropriate visualizations and pseudocode.
General Idea: Loop through a list of dictionaries and find the one with the highest value for "rating".
1) If the list is empty, return None
2) Assume the first book is the highest rated (so far)
3) For each book after the first:
a) If the book has a higher rating, set that book
as the new highest rated
4) Once all books are checked, return the highest rated book we found
⚠️ Common Mistakes
def highest_rated(books):
"
Returns the book with the highest rating from
a list of dictionaries representing books.
Each dictionary contains the book's title, author, and rating.
"
if not books: # Check if the list is empty
return None
highest_rated_book = books[0] # Assume the first book is the highest rated initially
for book in books[1:]: # Start checking from the second book
if book['rating'] > highest_rated_book['rating']:
highest_rated_book = book # Update the highest rated book
return highest_rated_book