How many machines are past calibration
Every machine has a last calibration date and a calibration interval in days. Count how many should have been recalibrated by today.
- A machine is overdue when today minus its last service is strictly greater than its interval.
- An interval of zero or less is a broken policy and counts as always overdue.
calibrationOverdue(machines: list<Machine>, today: 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 calibrationOverdue(machines []Machine, today int) int {
}
Worked examples
| Call | Result |
|---|---|
calibrationOverdue([]Machine{Machine{LastService: 100, IntervalDays: 10}, Machine{LastService: 95, IntervalDays: 10}, Machine{LastService: 110, IntervalDays: 10}}, 110) | 1 |
calibrationOverdue([]Machine{Machine{LastService: 0, IntervalDays: 0}}, 100) | 1 |
calibrationOverdue([]Machine{Machine{LastService: 100, IntervalDays: 20}, Machine{LastService: 100, IntervalDays: 20}}, 119) | 0 |
calibrationOverdue([]Machine{}, 50) | 0 |
Hint
Run the strict comparison, with the broken-policy case short-circuiting.
Reference solution in Go
func calibrationOverdue(machines []Machine, today int) int {
n := 0
for _, m := range machines {
if m.IntervalDays <= 0 || today-m.LastService > m.IntervalDays {
n++
}
}
return n
}