TactivergeLite

Lite / Building Machines That Respond / Week 9

Plan the sensor-log checker

You have one day's temperature log from a machine and one question: does it need an inspection? What is your plan before you type a line?

About 45 min · 3 goals · Checked by automated tests · Full interactive version

Why should I care?

The term project brings everything together: precise steps, types, formulas, decisions, loops, functions, tests and debugging. Engineers spend real time planning before coding, because a clear plan makes the code short and the testing obvious.

Predict

The brief says 'flag the machine if it gets too hot'. What should you do first?

  1. Start coding and decide later
  2. Turn it into a measurable rule, such as 'INSPECT if any reading is outside 10–60 °C or more than 2 lines are malformed'
  3. Ask Python to decide what 'too hot' means
Guess first, then reveal

Make it measurable first. A precise rule tells you what to code, what to test and what the boundary cases are (exactly 60 °C? exactly 2 malformed lines?).

Discover

Choose 'Sensor-log checker'. Order the six steps and mark the vague ones, then compare with the precise versions. Use the result as the skeleton of your plan: one function for each step that does real work.

What the interactive shows (text version)

The step orderer's 'Sensor-log checker' lists six steps to order: open the log and read it a line at a time; skip and count lines that do not convert; add good readings to a list; compute mean, minimum and maximum; count readings outside 10–60 °C; report INSPECT if any are outside or more than 2 lines were malformed, otherwise OK. Two steps start vague ('Skip the bad lines', 'Flag the machine if it seems too hot').

You can also open the interactive on the full site when you have a faster connection.

Understand

From brief to requirements

The brief: read a temperature log, flag readings outside a safe band, report the mean, minimum and maximum, and decide whether the machine needs inspection. Requirements: safe band 10–60 °C inclusive; a line that won't convert is malformed and counted; report INSPECT if any reading is outside the band or more than 2 lines are malformed, otherwise OK; an empty log reports 'no readings' and INSPECT.

Decompose into functions

parse_reading(line) → float or None. summarise(values) → (mean, min, max). count_outside(values, low_c, high_c) → int. decide(values, malformed) → "INSPECT" or "OK". A short main program reads the lines, calls these and prints the report. Write a one-sentence docstring for each before writing its body.

Design the test log to break it

Include: normal readings; exactly 10.0 and 60.0 (safe); 9.9 and 60.1 (outside); ERR, a blank and −999.0; exactly 2 and then 3 malformed lines; and an empty log. Write the expected report for each test log by hand, from the requirements, before running anything.

Explain it another way

Intuitive

Planning is drawing the map before the journey. It's quicker to fix a wrong turn on the map than halfway up the wrong mountain.

Technical

Requirements define observable behaviour; the decomposition defines interfaces (function signatures and contracts); the test plan defines how each requirement is verified, including equivalence classes and boundaries.

Applied

Condition-monitoring tools in factories implement this logic at scale: thresholds from the equipment manufacturer, data-quality counts and a simple, auditable decision rule.

Worked example 1

Test log: 25.0, 10.0, 60.0, ERR, ERR. What report should the checker give?

Good readings 25.0, 10.0, 60.0: mean 31.67 °C, min 10.0, max 60.0. Outside the band: 0 (10.0 and 60.0 are inside, inclusive). Malformed: 2, which is not more than 2. So the decision is OK. Add one more ERR and it must become INSPECT: a boundary test on the malformed count.

Common mix-ups

Practice

Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.

%

Practice 2. Which test log best checks the safe-band boundary?
Practice 3. What should the checker do with an empty log?
Practice 4. Why write each function's docstring before its body?

Apply it

Write your project plan: the requirements (with numbers), the four function signatures with docstrings, a flowchart or numbered algorithm for the main program, and at least three test logs with their expected reports worked out by hand.

Reflect

Which requirement in the brief was the most ambiguous, and how did you make it precise?

Write your answer on paper or in your own notes. Lite never stores what you write.

Go further

← Catch bad data before it bitesBuild it, test it, break it →