Drill

ProblemsGo › logistics

What does this parcel cost to send

easylogisticsMathGo

A courier prices every parcel as: the first five kilograms cost 300 (minor units) and each further kilogram adds 60.

parcelCost(weight: int) → int

Go 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

func parcelCost(weight int) int {
	
}

Worked examples

CallResult
parcelCost(3)300
parcelCost(5)300
parcelCost(6)360
parcelCost(10)600

Hint

Subtract the free five, multiply what is left, and add the base.

Reference solution in Go
func parcelCost(weight int) int {
	if weight < 0 {
		return 0
	}
	extra := 0
	if weight > 5 {
		extra = weight - 5
	}
	return 300 + extra*60
}

The same problem in another language

More logistics problems in Go