Drill

ProblemsPython › billing

Apply credits to an invoice

mediumbillingPython

Customer credits are applied against an invoice in order. Return the remaining unpaid balance.

credit_first(invoice_total: int, credits: list<Credit>) → int

Solve it in the editor →

Where you start

def credit_first(invoice_total: int, credits: list[Credit]) -> int:
    

Worked examples

CallResult
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

The same problem in another language

More billing problems in Python