Compute the final charge on an order
Combine a subtotal, an optional discount, and shipping to produce the final amount due.
- Subtract the discount from the subtotal, clamped at zero.
- Then add the shipping cost.
final_charge(subtotal: int, discount: int, shipping: int) → int
Where you start
def final_charge(subtotal: int, discount: int, shipping: int) -> int:
Worked examples
| Call | Result |
|---|---|
final_charge(5000, 500, 800) | 5300 |
final_charge(1000, 2000, 500) | 500 |
final_charge(3000, 0, 1000) | 4000 |
final_charge(0, 0, 0) | 0 |
Hint
Max(0, subtotal − discount) then add shipping.
Reference solution in Python
def final_charge(subtotal: int, discount: int, shipping: int) -> int:
net = subtotal - discount if subtotal > discount else 0
return net + shipping