Parcel discount band
A monthly contract discounts each parcel once the month's total reaches a threshold: every parcel from then on ships 15% cheaper.
- Ordinary rate applies below the threshold.
- A parcel is discounted once the running total, including it, has reached the threshold.
- The discounted rate is floor(cost × 85 / 100).
bulkDiscountBand(parcelCosts: list<int>, threshold: 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 bulkDiscountBand(parcelCosts []int, threshold int) int {
}
Worked examples
| Call | Result |
|---|---|
bulkDiscountBand([]int{100, 100, 100, 100}, 250) | 370 |
bulkDiscountBand([]int{100, 100}, 200) | 185 |
bulkDiscountBand([]int{100, 100, 100}, 1000) | 300 |
bulkDiscountBand([]int{}, 0) | 0 |
Hint
Walk the values; once the running total crosses the threshold everything from that parcel on is discounted.
Reference solution in Go
func bulkDiscountBand(parcelCosts []int, threshold int) int {
total, running := 0, 0
for _, cost := range parcelCosts {
running += cost
if running >= threshold {
total += cost * 85 / 100
} else {
total += cost
}
}
return total
}