question 253 Classes & Objects · easy
Review summary through Review objects
Product reviews arrive as rows of text. Write a class Review that turns one row's values into a review with an int rating and a positive attribute (True for a rating of 4 or 5). Then write review_summary(rows), which builds a Review from every row and returns, for each product, how many reviews it has and how many of them are positive.
Class
Review(product_id, rating, comment): attributes product_id, rating (int), comment, positive (rating ≥ 4)Function
review_summary(rows); rows are {"product_id": …, "rating": "5", "comment": …}Returns
{product_id: [review count, positive count]}, products in first-seen order; {} for no rowsExamples
callreview_summary([{'product_id': 'P0205', 'rating': '5', 'comment': 'Great mat'}, {'product_id': 'P0421', 'rating': '2', 'comment': 'Too thin'}, {'product_id': 'P0205', 'rating': '3', 'comment': 'OK'}, {'product_id': 'P0205', 'rating': '4', 'comment': 'Good grip'}])returns{'P0205': [3, 2], 'P0421': [1, 0]}
callreview_summary([{'product_id': 'P0164', 'rating': '4', 'comment': 'Fine'}])returns{'P0164': [1, 1]}
callreview_summary([])returns{}