Transfers Between Accounts
ops is a list of bank operations, applied in order. Every account starts at balance 0 and exists from the first operation that mentions it, even if that operation is rejected. The operations are:
"deposit NAME N": addNto the balance ofNAME."withdraw NAME N": takeNfromNAMEif its balance is at leastN; otherwise the operation is rejected and nothing changes."transfer FROM TO N": moveNfromFROMtoTOif the balance ofFROMis at leastN; otherwise the operation is rejected and nothing changes.FROMandTOare always different.
Return a dictionary mapping every account that was mentioned to its final balance (zero balances included). No operations gives an empty dictionary.
Example 1
ops ["deposit ann 50","withdraw ann 20","transfer ann bob 25","withdraw bob 30"]
returns {"ann":5,"bob":25}
ann gets 50, loses 20, then sends 25 to bob and is left with 5. bob has 25, so his withdrawal of 30 is rejected.
Example 2
ops ["transfer ann bob 10"]
returns {"ann":0,"bob":0}
ann has nothing, so the transfer is rejected, but both accounts now exist with balance 0.
Constraints
0 <= len(ops) <= 1001 <= N <= 100- account names are 1 to 8 lowercase letters
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.