Price per unit
A bulk bin lists a total for a quantity of identical items. What does a single unit cost?
- Price per unit is total ÷ quantity, rounded down to whole minor units.
- A quantity of zero or less has no per-unit price: return 0.
pricePerUnit(totalMinor: int, quantity: 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 pricePerUnit(totalMinor int, quantity int) int {
}
Worked examples
| Call | Result |
|---|---|
pricePerUnit(1000, 4) | 250 |
pricePerUnit(1000, 3) | 333 |
pricePerUnit(100, 10) | 10 |
pricePerUnit(0, 5) | 0 |
Hint
Divide and drop the remainder.
Reference solution in Go
func pricePerUnit(totalMinor int, quantity int) int {
if quantity <= 0 {
return 0
}
return totalMinor / quantity
}