Drill

ProblemsC# › orders

Work out the shipping charge

mediumordersMathC#

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

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

C# needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.

Solve it in Python →

Where you start

public int ShippingCost(int grams, bool express) {
    
}

Worked examples

CallResult
ShippingCost(400, false)3000
ShippingCost(500, false)3000
ShippingCost(1500, false)5000
ShippingCost(5000, false)8000

Hint

For the top band, round the excess up to whole kilos: (excess + 999) / 1000 in integer arithmetic.

Reference solution in C#
public int ShippingCost(int grams, bool express) {
    if (grams <= 0) return 0;
    int cost;
    if (grams <= 500) cost = 3000;
    else if (grams <= 2000) cost = 5000;
    else if (grams <= 5000) cost = 8000;
    else cost = 8000 + ((grams - 5000 + 999) / 1000) * 1500;
    return express ? cost * 2 : cost;
}

The same problem in another language

More orders problems in C#