Fix the rating summary for a restaurant dashboard

Review the AI's codeEasy12 min

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, written restaurant_id|rating. A rating of NULL means the reviewer gave none.
  • Return one [average_in_hundredths, review_count] pair per entry of restaurant_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 in restaurant_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, NULL included, 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) // n for 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
Inputsummarize_ratings(["r1", "r2"], ["r1|5", "r1|1", "r2|4"])
Output[[300, 2], [400, 1]]
Example 2
Inputsummarize_ratings(["r1", "r_new"], ["r1|4", "r1|5"])
Output[[450, 2], [-1, 0]]
Example 3
Inputsummarize_ratings(["r1"], ["r1|NULL", "r1|3"])
Output[[300, 2]]