Drill

ProblemsC++ › machines

How many machines are past calibration

mediummachinesArraysMathC++

Every machine has a last calibration date and a calibration interval in days. Count how many should have been recalibrated by today.

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.

Solve it in Python →

Where you start

int calibrationOverdue(std::vector<Machine> machines, int today) {
    
}

Worked examples

CallResult
calibrationOverdue(std::vector<Machine>{Machine{100, 10}, Machine{95, 10}, Machine{110, 10}}, 110)1
calibrationOverdue(std::vector<Machine>{Machine{0, 0}}, 100)1
calibrationOverdue(std::vector<Machine>{Machine{100, 20}, Machine{100, 20}}, 119)0
calibrationOverdue(std::vector<Machine>{}, 50)0

Hint

Run the strict comparison, with the broken-policy case short-circuiting.

Reference solution in C++
int calibrationOverdue(std::vector<Machine> machines, int today) {
    int n = 0;
    for (const auto& m : machines) {
        if (m.intervalDays <= 0 || today - m.lastService > m.intervalDays) n++;
    }
    return n;
}

The same problem in another language

More machines problems in C++