easy +8 pts

Update Row by ID

Parse CSV-like rows and update the first occurrence of a given ID.

You are given a list of strings, each representing a CSV row with columns: id,name,score. The id is a positive integer, name is a string (no commas), and score is a positive integer. Write a function `update_row_by_id(rows, target_id, new_score)` that processes the rows and returns a new list of strings with the same format `id,name,score`. If exactly one row has the id equal to `target_id`, replace its score with `new_score`. If no row matches or more than one row matches, return the original list unchanged. You must not modify the input list. Function signature: `def update_row_by_id(rows: list[str], target_id: int, new_score: int) -> list[str]`

Constraints

- 0 <= len(rows) <= 1000 - Each row is a valid CSV string with exactly three fields: id (positive int), name (non-empty string without commas), score (positive int). - target_id is a positive int. - new_score is a positive int. - Complexity not strict, but aim for O(n) time and O(n) memory.

Example

>>> update_row_by_id(["1,Alice,90", "2,Bob,85"], 2, 95)
['1,Alice,90', '2,Bob,95']
>>> update_row_by_id(["1,Alice,90", "2,Bob,85"], 3, 95)
['1,Alice,90', '2,Bob,85']
>>> update_row_by_id([], 1, 100)
[]
8 points ~12 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Count occurrences of target_id before modifying.
Use split(',') to parse each row.
Build new row with updated score only when exactly one match exists.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.