Fix the loop that explains a reduce one-liner

Review the AI's codeEasy10 min

Your task

An AI fixed a bug with a one-line functools.reduce call and a lambda. The test passed, but you could not explain the line, and your team only merges code its author can explain. So you asked for the same logic as a plain loop that records the running balance after every step.

An assistant wrote explain_fold_steps(values). It is in the editor. It has a bug. Find it and fix it.

Input and output:

  • values: a list of integers. Any of them can be negative.
  • Return a list with one entry per value, in order: the balance right after that value is applied.

Rules the tests check:

  • The balance starts at 0. That starting 0 is not part of the output, so an empty list returns an empty list.
  • For each value in order: add it to the balance. If the result is below 0, the balance becomes 0. Then record the balance.

Examples

explain_fold_steps(values: list[int]) -> list[int]
Example 1
Inputexplain_fold_steps([3, -5, 4])
Output[3, 0, 4]
Example 2
Inputexplain_fold_steps([1, 2, 3])
Output[1, 3, 6]