Apply credits to an invoice
Customer credits are applied against an invoice in order. Return the remaining unpaid balance.
- Apply credits in list order, each subtracting from the running balance.
- The balance is clamped at 0 — credits never produce a negative result.
credit_first(invoice_total: int, credits: list<Credit>) → int
Where you start
def credit_first(invoice_total: int, credits: list[Credit]) -> int:
Worked examples
| Call | Result |
|---|---|
credit_first(10000, [Credit(amount=3000), Credit(amount=5000)]) | 2000 |
credit_first(5000, [Credit(amount=3000)]) | 2000 |
credit_first(2000, [Credit(amount=5000)]) | 0 |
credit_first(0, [Credit(amount=1000)]) | 0 |
Hint
Walk the credits, subtract each, and clamp at each step.
Reference solution in Python
def credit_first(invoice_total: int, credits: list[Credit]) -> int:
balance = invoice_total
for c in credits:
balance -= c.amount
if balance < 0:
balance = 0
return balance