What does this parcel cost to send
A courier prices every parcel as: the first five kilograms cost 300 (minor units) and each further kilogram adds 60.
- A parcel at or under five kilograms always costs 300.
- Every whole kilogram over five adds 60; there are no fractions here.
- A parcel cannot weigh negative kilograms: return 0 for bad input.
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.
Where you start
func parcelCost(weight int) int {
}
Worked examples
| Call | Result |
|---|---|
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
}