Fix the rating summary for a restaurant dashboard
Your task
An AI's SQL for a ratings dashboard used an inner join, a WHERE rating != 1 filter and COUNT(*). Some restaurants showed suspiciously high averages and new ones vanished from the list. The rewrite computes each restaurant's numbers the way SQL's LEFT JOIN, AVG and COUNT should have.
An assistant wrote summarize_ratings(restaurant_ids, review_lines). It is in the editor. It has a bug. Find it and fix it.
Input and output:
restaurant_ids: the restaurants to show, in order.review_lines: one review per string, writtenrestaurant_id|rating. A rating ofNULLmeans the reviewer gave none.- Return one
[average_in_hundredths, review_count]pair per entry ofrestaurant_ids, in the same order. A repeated id gets a repeated pair.
Rules the tests check:
- Skip a review that does not split on
|into exactly 2 parts, or whose restaurant is not inrestaurant_ids. - Every other review counts toward its restaurant's
review_count, whatever its rating says. - A rating is 1 to 15 digits from 0 to 9. Anything else,
NULLincluded, is unknown: the review still counts, but it is left out of the average. - No rating value is left out of the average: 1-star reviews count like any other.
- The average, in hundredths, is the sum of the known ratings times 100, divided by how many there are, rounded to the nearest whole number with halves rounded up:
(sum * 100 + n // 2) // nfor n known ratings. - With no known ratings, the average is -1. A restaurant with no reviews at all gets
[-1, 0].
Examples
summarize_ratings(restaurant_ids: list[str], review_lines: list[str]) -> list[list[int]]Example 1
Input
summarize_ratings(["r1", "r2"], ["r1|5", "r1|1", "r2|4"])Output
[[300, 2], [400, 1]]Example 2
Input
summarize_ratings(["r1", "r_new"], ["r1|4", "r1|5"])Output
[[450, 2], [-1, 0]]Example 3
Input
summarize_ratings(["r1"], ["r1|NULL", "r1|3"])Output
[[300, 2]]