Lite / Building Machines That Respond / Week 6
Count, flag and find extremes
A greenhouse thermometer's daily mean is a perfect 22.0 °C, yet it reported −40 °C and 85 °C. How could a mean hide that?
Why should I care?
Summaries are only useful if they reveal problems. A loop that counts readings over a limit, tracks the extremes and flags impossible values turns a wall of numbers into a decision: fine, inspect, or replace the sensor.
- Combine a loop and an if to count readings that meet a condition.
- Find the maximum and minimum with a loop, starting from the first reading.
- Flag implausible readings (glitches) and explain why a mean alone can mislead.
Predict
Readings: 21.4, 21.6, −40.0, 21.9, 22.1, 85.0, 22.0 °C. What is the mean?
- About 21.8 °C
- Exactly 22.0 °C
- About 30 °C
Guess first, then reveal
Exactly 22.0 °C: the −40 and the 85 happen to cancel out. The mean looks normal, but two readings are impossible for a greenhouse. Always look at the extremes and count glitches as well.
Discover
Open the Smart Greenhouse at Level 2 ('Read the data'). Find the glitch in the ten noisy readings and the mean without it. While you work, write the loop you are running in your head: what do you count, what do you compare, what do you skip?
What the interactive shows (text version)
The Smart Greenhouse Level 2 task gives ten thermometer readings, one of them an obvious glitch, and asks for the mean without it. On paper: list the readings, cross out any outside a plausible band, add the rest and divide by how many remain. In this lesson's example, 21.4, 21.6, −40.0, 21.9, 22.1, 85.0, 22.0 has mean 22.0, max 85.0, min −40.0 and 2 glitches.
Understand
Count with an if inside a loop
readings_c = [21.4, 21.6, -40.0, 21.9, 22.1, 85.0, 22.0]
hottest = readings_c[0]
coldest = readings_c[0]
glitches = 0
for r in readings_c:
if r > hottest:
hottest = r
if r < coldest:
coldest = r
if r < -10 or r > 50:
glitches += 1
print(hottest, coldest, glitches) # 85.0 -40.0 2Why start from the first reading?
Starting hottest = 0 fails when every reading is below 0 °C, as in a freezer log: the 'hottest' would wrongly stay 0. Starting from readings_c[0] (or None, then filling it on the first pass) always gives a real reading. Python's max() and min() do the same and should agree with your loop.
Plausibility checks catch glitches
A greenhouse is never −40 °C. A plausibility band (here −10 to 50 °C) flags readings that are physically impossible, usually from a loose wire, electrical noise or a sensor fault. Flag and count them, report them, and leave them out of the mean, rather than silently averaging them in.
Explain it another way
Intuitive
The mean is like the average height of a class: it can't tell you that one person is on stilts and another is sitting on the floor.
Technical
Each pass may update several accumulators: counters, running extrema and flags. The extrema must be initialised from the data, not from a constant, to be correct for every input range.
Mathematical
The mean is sensitive to outliers: one value of 85 among six near 22 adds (85 − 22)/7 ≈ 9 °C to it. The minimum and maximum, and a count of out-of-band values, expose what the mean hides.
Applied
Industrial data pipelines run plausibility and range checks on every sensor channel before using the data, and raise a maintenance alert when the glitch count rises.
Worked example 1
Freezer log: −19.5, −18.0, −21.2, −18.9 °C. A program starts hottest = 0. What does it report, and how do you fix it?
No reading is above 0, so hottest stays 0 °C: wrong. Start with hottest = readings[0] = −19.5; the loop then finds −18.0 °C, the real maximum.
Common mix-ups
If the mean is normal, the data is fine.
✓ A mean can hide extreme or impossible readings. Check the max, min and plausibility too.Glitchy readings should be quietly deleted.
✓ Flag, count and report them. A rising glitch count is itself a warning that a sensor is failing.
Practice
Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.
Apply it
Extend your log program: report the mean of only the plausible readings, the max and min, and the number of glitches. Finish the Smart Greenhouse Level 2 task and check your program gives the same mean without the glitch.
Reflect
What would you want a maintenance engineer to see first in your log report, and why?
Write your answer on paper or in your own notes. Lite never stores what you write.