Early payment discount
A vendor rewards early payment: the further ahead the payment, the larger the discount off the subtotal.
- Days early >= 10 earns a 2% discount, rounded down.
- Days early >= 5 (but under 10) earns a 1% discount, rounded down.
- Under 5 days earns no discount.
- The discount is floor(subtotal × percentage / 100).
earlyDiscount(subtotal: int, daysEarly: 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 earlyDiscount(subtotal int, daysEarly int) int {
}
Worked examples
| Call | Result |
|---|---|
earlyDiscount(10000, 10) | 200 |
earlyDiscount(5000, 5) | 50 |
earlyDiscount(7500, 15) | 150 |
earlyDiscount(999, 3) | 0 |
Hint
Check the thresholds from largest to smallest.
Reference solution in Go
func earlyDiscount(subtotal int, daysEarly int) int {
if daysEarly >= 10 {
return subtotal * 2 / 100
}
if daysEarly >= 5 {
return subtotal / 100
}
return 0
}