A Wallet That Says No

Python for AIMedium+10 XP+10

A Wallet That Says No

Part 1: Wallet(balance)

  • add(amount) adds money to balance.
  • spend(amount): if amount is more than the balance, change nothing and return False. Otherwise take it off the balance and return True. Spending exactly what's there is allowed.

Part 2: run_wallet(start, moves)

Make a wallet with start money. moves is a list of pairs like ("add", 10) or ("spend", 4). Apply them in order. Return a list of two numbers: [final balance, how many spends were refused].

Examples

Wallet(3).spend(5)
returns False
start = 10
moves = [('spend', 4), ('spend', 9)]
returns [6, 1]
start = 20
moves = [("spend", 5), ("add", 10), ("spend", 30)]
returns [25, 1]
start = 0
moves = []
returns [0, 0]

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run