Workspace/Coding labs
Loading progress

Reserve and settle a shared budget

Intermediate65 min

Simulate a sequential ledger with integer credits. Reserve atomically in this simulation if available capacity is sufficient; otherwise append False to decisions. An identical active reservation appends True without double reserving. Settlement charges at most the reservation and releases its remainder. Identical settlement is idempotent; conflicting reuse fails. Cancel only represents confirmed **pre-dispatch** cancellation and releases capacity; repeated cancel is harmless. Closed IDs cannot be reserved again. Unknown operations, negative values, and overruns raise ValueError. Production concurrency and uncertain provider usage are outside this simulator.

Your task

  1. Complete the starter function using the contract above.
  2. Use the examples and visible tests to check normal inputs, boundaries, and rejected inputs.
  3. Run tests to record your result, then compare with the explained reference solution.

Examples

EXAMPLE 1

Inputlimit 10; reserve a=7; reserve b=4

Outputdecisions [True,False], available 3

Active work already consumes admission capacity.
EXAMPLE 2

Inputreserve a=7; settle a=5; reserve b=4

Outputspent 5, reserved 4, available 1

Unused reservation becomes available after confirmed settlement.
solution.pyPython 3.12