Fix the transfer runner for account balances

Review the AI's codeEasy12 minStripe style

Your task

An AI's Java transfer method read both balances, checked for enough funds, then wrote both back, all inside @Transactional. Under load, two transfers read the same balance, both passed the check, and one debit was lost. The fix makes the check and the debit one step against the current balance. You model it by applying a list of transfers in order.

An assistant wrote apply_transfers(balances, transfers). It is in the editor. It has a bug. Find it and fix it.

Input and output:

  • balances: each account's balance, in cents. The account's number is its index.
  • transfers: the transfers to apply, in order, each written from,to,amount.
  • Return the final balances.

Rules the tests check:

  • Skip a transfer unless all of these hold:
    • it splits on , into exactly 3 parts, each an optional - followed by one or more digits from 0 to 9
    • from and to are valid indexes into balances, and they differ
    • amount is above 0
    • the from account's current balance, after every earlier transfer, is at least amount
  • Otherwise move amount from the from account to the to account.

Examples

apply_transfers(balances: list[int], transfers: list[str]) -> list[int]
Example 1
Inputapply_transfers([1000, 500], ["0,1,300"])
Output[700, 800]
Example 2
Inputapply_transfers([100, 100], ["0,1,150"])
Output[100, 100]
Example 3
Inputapply_transfers([100, 0], ["0,1,100", "0,1,1"])
Output[0, 100]