Sensors That Wobble Together

Data with PythonMedium+20 XP+20

Sensors That Wobble Together

A greenhouse has a line of sensors, and every hour each one sends in a raw reading. The readings arrive as a grid: one row per hour, one column per sensor.

The raw numbers can't be compared yet, for two reasons:

  1. Each sensor has its own gain. Every sensor reads in slightly different units, so its raw value has to be multiplied by its own gain. gain[s] belongs to sensor s, so it applies to that sensor's value in every hour.
  2. Each hour has its own wobble. Once the gains are applied, a flicker in the power supply pushes every sensor in that hour up or down by the same unknown amount. Taking the hour's average away from every value in that hour cancels the wobble out.

Task: write calibrate(readings, gain), which returns a new grid of the same shape:

  • first multiply each value by its sensor's gain;
  • then, for each hour, work out the average of that hour's calibrated values and subtract it from every calibrated value in that hour.

readings is a list of rows (one list of numbers per hour) and gain holds one number per sensor. Return a list of lists of plain Python floats, each rounded to 4 decimal places.

For example, with gains [2.0, 0.5, 1.0], an hour that reads [10, 20, 30] calibrates to [20, 10, 30]. Its average is 20, so that hour becomes [0.0, -10.0, 10.0].

Every row of the answer adds up to 0 — that is what taking away a row's own average does. It makes a quick check that you centred in the right direction.

The strip has one value for each row.

Examples

readings = [[10, 20, 30], [12, 18, 33]]
gain = [2.0, 0.5, 1.0]
returns [[0.0, -10.0, 10.0], [2.0, -13.0, 11.0]]
readings = [[4, 7], [5, 5], [8, 1]]
gain = [1.5, 0.5]
returns [[1.25, -1.25], [2.5, -2.5], [5.75, -5.75]]

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run