Fix the payout consumer that skips repeat deliveries
Your task
An assistant's SQS consumer credited a payout every time a message arrived. SQS standard queues deliver at least once, so a message can come back after a timeout or a crash, and the account gets paid twice. The fix keeps a record of the payout ids already applied.
An assistant wrote apply_payouts(account_ids, starting_balances, deliveries). It is in the editor. It has a bug. Find it and fix it.
Input and output:
account_ids: the account ids, as strings.starting_balances: integers, the same length asaccount_ids. Each is the balance of the account at the same position.deliveries: the messages in the order they arrive, each meant to be"payout_id|account_id|amount". The same message can arrive more than once.- Return the final balances, in the order of
account_ids.
Rules the tests check:
- Skip a delivery that does not split on
|into exactly 3 parts. - Skip it when the payout id is empty, or when that payout id has already been applied.
- Skip it when the account id is not in
account_ids. If an id appears more than once there, use the first one. - Skip it unless the amount is 1 to 12 characters, every one a digit from 0 to 9, and its value is above 0. Leading zeros are fine.
- Otherwise add the amount to that account's balance, and record the payout id as applied.
- A skipped delivery records nothing, so a later delivery with the same payout id can still be applied.
Examples
apply_payouts(account_ids: list[str], starting_balances: list[int], deliveries: list[str]) -> list[int]Example 1
Input
apply_payouts(["acct-1", "acct-2"], [100, 50], ["p1|acct-1|25", "p1|acct-1|25", "p2|acct-2|10"])Output
[125, 60]Example 2
Input
apply_payouts(["acct-1"], [0], ["p1|acct-1|5", "p2|acct-1|5"])Output
[10]