Pre-Calculus for Programmers

Quality control report

Quality control report

A workshop measures every pin it cuts and logs the length in micrometres, which is why all the numbers are whole. A pin is acceptable if its length is within tol of target, too long and too short counting the same. At the end of a shift somebody wants the list of the ones that failed, so they can be pulled off the line.

Given readings, target and tol, return the readings that failed, in the order they were measured. A reading exactly tol away from the target passes: the specification says within, and within includes the boundary.

Worked example

Input: readings = [498, 500, 502, 505], target = 500, tol = 2

  498: distance |498 - 500| = 2, which is not more than 2, so it passes
  500: distance 0, passes
  502: distance 2, passes
  505: distance 5, which is more than 2, so it fails

Output: [505]
A target of 500 with a tolerance of 2.

The awkward ends

Input: readings = [], target = 50, tol = 3         Output: []
Input: readings = [100], target = 100, tol = 0     Output: []
Input: readings = [10, -10, 0], target = 0, tol = 5  Output: [10, -10]
Nothing measured, nothing failing, and everything failing.

Constraints

  • readings holds between 0 and 2000 values, each from −106-10^{6} to 10610^{6}.
  • target is from −106-10^{6} to 10610^{6}, and tol is from 0 to 10610^{6}.
  • The order of the failures matters: report them in the order they were measured.
  • Readings may be negative, and the target may be too, so the distance has to be worked out with an absolute value rather than by subtracting the smaller from the larger.
Code

Run your code to see how it does on the examples. Submit also runs the hidden cases.