Work out the shipping charge
The carrier bills in weight bands, and anything over five kilos is charged per extra kilo started.
- Up to 500 g costs 3000. Up to 2 kg, 5000. Up to 5 kg, 8000.
- Above 5 kg it is 8000 plus 1500 for every extra kilo begun — 5100 g pays for one extra kilo, 6001 g pays for two.
- Express doubles whatever the total came to.
- Zero or negative weight costs nothing, express or not.
shipping_cost(grams: int, express: bool) → int
Where you start
def shipping_cost(grams: int, express: bool) -> int:
Worked examples
| Call | Result |
|---|---|
shipping_cost(400, False) | 3000 |
shipping_cost(500, False) | 3000 |
shipping_cost(1500, False) | 5000 |
shipping_cost(5000, False) | 8000 |
Hint
For the top band, round the excess up to whole kilos: (excess + 999) / 1000 in integer arithmetic.
Reference solution in Python
def shipping_cost(grams: int, express: bool) -> int:
if grams <= 0:
return 0
if grams <= 500:
cost = 3000
elif grams <= 2000:
cost = 5000
elif grams <= 5000:
cost = 8000
else:
cost = 8000 + ((grams - 5000 + 999) // 1000) * 1500
return cost * 2 if express else cost