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
C# 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
public int CalibrationOverdue(List<Machine> machines, int today) {
}
Worked examples
| Call | Result |
|---|---|
CalibrationOverdue(new List<Machine> { new Machine(100, 10), new Machine(95, 10), new Machine(110, 10) }, 110) | 1 |
CalibrationOverdue(new List<Machine> { new Machine(0, 0) }, 100) | 1 |
CalibrationOverdue(new List<Machine> { new Machine(100, 20), new Machine(100, 20) }, 119) | 0 |
CalibrationOverdue(new List<Machine> { }, 50) | 0 |
Hint
Run the strict comparison, with the broken-policy case short-circuiting.
Reference solution in C#
public int CalibrationOverdue(List<Machine> machines, int today) {
int n = 0;
foreach (var m in machines) {
if (m.IntervalDays <= 0 || today - m.LastService > m.IntervalDays) n++;
}
return n;
}