Fix the replay of inputs after a list delete

Review the AI's codeEasy10 min

Your task

A list renders each row with key={i}, the array index, and an uncontrolled input (one that keeps its own text in the DOM). After someone deletes a middle row, typed text shows up in the wrong rows, and the team wants a replay of what each input shows afterwards.

An assistant wrote visible_input_texts(item_ids, delete_index, key_by_index). It is in the editor. It has a bug. Find it and fix it.

Input and output:

  • item_ids: the text each row's input shows before the delete, in render order. Each row starts with its own id typed in. Two rows can show the same text.
  • delete_index: the position of the row being deleted. It can be any integer, including a negative one.
  • key_by_index: true when the rows are keyed by array index, false when they are keyed by a stable, unique item id.
  • Return the texts the inputs show after the delete, in render order.

Rules the tests check:

  • When delete_index is below 0, or not below the number of rows, nothing is deleted: return the texts unchanged. A negative index never counts from the end.
  • Keyed by index: React matches rows by position. Every position but the last keeps its DOM node and its text, and the last row's node goes. Return every text but the last one, whichever row was deleted.
  • Keyed by id: exactly the row at delete_index goes, and every other row keeps its own text. Return the texts with that one position removed.
  • No rows returns an empty list.

Examples

visible_input_texts(item_ids: list[str], delete_index: int, key_by_index: bool) -> list[str]
Example 1
Inputvisible_input_texts(["a", "b", "c", "d"], 1, True)
Output["a", "b", "c"]
Example 2
Inputvisible_input_texts(["a", "b", "c", "d"], 1, False)
Output["a", "c", "d"]
Example 3
Inputvisible_input_texts(["a", "b", "c", "d"], 3, True)
Output["a", "b", "c"]