Payment method surcharge
Certain payment methods carry a surcharge. Look up the percentage and apply it to the subtotal.
- Rates in the map are basis points — hundredths of a percent.
- The surcharge is floor(subtotal × rate / 10000).
- An unknown method means no surcharge.
surcharge_by_method(surcharge_rates: map<string, int>, method: string, subtotal: int) → int
Where you start
def surcharge_by_method(surcharge_rates: dict[str, int], method: str, subtotal: int) -> int:
Worked examples
| Call | Result |
|---|---|
surcharge_by_method({"visa": 150, "amex": 300}, "amex", 10000) | 300 |
surcharge_by_method({"visa": 150}, "paypal", 5000) | 0 |
surcharge_by_method({"visa": 150}, "visa", 8000) | 120 |
surcharge_by_method({}, "visa", 1000) | 0 |
Hint
Map lookup, then a single integer division by ten thousand.
Reference solution in Python
def surcharge_by_method(surcharge_rates: dict[str, int], method: str, subtotal: int) -> int:
rate = surcharge_rates.get(method, 0)
return subtotal * rate // 10000