Drill

ProblemsPython › orders

Work out the shipping charge

mediumordersPython

The carrier bills in weight bands, and anything over five kilos is charged per extra kilo started.

shipping_cost(grams: int, express: bool) → int

Solve it in the editor →

Where you start

def shipping_cost(grams: int, express: bool) -> int:
    

Worked examples

CallResult
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

The same problem in another language

More orders problems in Python