Refund for partially used service
A customer paid upfront but only used part of the service. Refund the unused portion.
- Refund equals paid minus (usedUnits × unitPrice), clamped at zero.
- If usage cost meets or exceeds what was paid, no refund.
partial_refund(paid: int, used_units: int, unit_price: int) → int
Where you start
def partial_refund(paid: int, used_units: int, unit_price: int) -> int:
Worked examples
| Call | Result |
|---|---|
partial_refund(1000, 40, 10) | 600 |
partial_refund(1000, 200, 10) | 0 |
partial_refund(500, 0, 50) | 500 |
partial_refund(1000, 100, 10) | 0 |
Hint
Multiply usage by price, subtract from what was paid, and clamp.
Reference solution in Python
def partial_refund(paid: int, used_units: int, unit_price: int) -> int:
cost = used_units * unit_price
return paid - cost if paid > cost else 0