How much of this can still be refunded
An agent asks to refund an amount against an order that may already have been partly refunded.
- Never refund more than what is left of the original payment.
- Never return a negative figure, whatever the inputs say.
- A request for zero or less refunds nothing.
refundable_amount(paid: int, refunded: int, requested: int) → int
Where you start
def refundable_amount(paid: int, refunded: int, requested: int) -> int:
Worked examples
| Call | Result |
|---|---|
refundable_amount(1000, 200, 500) | 500 |
refundable_amount(1000, 200, 900) | 800 |
refundable_amount(1000, 1000, 50) | 0 |
refundable_amount(1000, 0, -5) | 0 |
Hint
Work out what remains, then take the smaller of that and the request — floored at zero.
Reference solution in Python
def refundable_amount(paid: int, refunded: int, requested: int) -> int:
if requested <= 0:
return 0
left = paid - refunded
if left <= 0:
return 0
return min(left, requested)