──────────────────────────────────────

60. Radio Shift Diagnostics · Build a Broadcast Plan Check

Check a radio plan’s total and run the same report with a fake clock or a real clock.

Lesson files · lessons/debug-and-measure/broadcast-plan-check

Before you start
- Trace a running total and verify it against a result calculated by hand.
- Use a fake clock to measure a call reproducibly.
- Use wraps to add behaviour while forwarding arguments and results.

By the end
- Fix a faulty running total and prove the repair on the case that exposed it.
- Add a single-call wrapper that logs the function name and elapsed milliseconds.
- Print a clear match or mismatch report using an independent expected result.

Where you can use it
A programme planner can catch a wrong total before booking a broadcast slot and measure the check when working on a slower device.

A planned running time gives you a result to compare with the latest script output. Keep that expected value independent of the calculation you are checking. Run the starter to see the wrong total reported as a mismatch. Then repair the calculation and rerun that same case before trying a different plan.

Keep the timing and trace in the wrapper. It should tell you which function ran and how long it took, then give the result back to the caller. The caller compares the numbers. Call with_timing(total_minutes, clock=fake_clock) to supply test readings. Leaving out the clock uses perf_counter for a real run.

Predict
Which status should the station volunteer see for this run?
- MATCH
- MISMATCH

Worked example (example.py)

expected = 8
actual = 3
print(f"Expected: {expected}")
print(f"Actual: {actual}")
status = "MATCH" if actual == expected else "MISMATCH"
print(f"Status: {status}")


Example result
Expected: 8
Actual: 3
Status: MISMATCH

Run the sample to reproduce the wrong total, then fix total_minutes. Complete with_timing so it prints “Running total_minutes”, calls the work once, prints “Elapsed: N ms” with a whole number, and returns the work’s result. Preserve the name with wraps. Enter space-separated durations on line one, the hand-checked total on line two, and two clock readings in seconds on line three. Keep the Expected, Actual and Status lines. Demonstrate a match and a mismatch.

Starter program (starter.py)

from functools import wraps
from time import perf_counter


def total_minutes(durations):
    total = 0
    for minutes in durations:
        total = minutes
    return total


def with_timing(function, clock=perf_counter):
    return function


durations = [int(value) for value in input().split()]
expected = int(input())
readings = iter(float(value) for value in input().split())


def fake_clock():
    return next(readings)


run_total = with_timing(total_minutes, clock=fake_clock)
actual = run_total(durations)
print(f"Expected: {expected}")
print(f"Actual: {actual}")
status = "MATCH" if actual == expected else "MISMATCH"
print(f"Status: {status}")


Starter input
5 0 3
8
10 10.25

Hint
Pass through *args and **kwargs and take two clock readings around the single call. Use :.0f to round milliseconds for display.

Case 1. Try the sample
Input
5 0 3
8
10 10.25
Expected output
Running total_minutes
Elapsed: 250 ms
Expected: 8
Actual: 8
Status: MATCH

Case 2. The plan gives the wrong expectation
Input
9 2
2
10 10.5
Expected output
Running total_minutes
Elapsed: 500 ms
Expected: 2
Actual: 11
Status: MISMATCH

Case 3. An empty broadcast plan
Input
 
0
9 9
Expected output
Running total_minutes
Elapsed: 0 ms
Expected: 0
Actual: 0
Status: MATCH

Project checklist
Run the sample case.

Project checklist
Try one different case and explain what happened.

Marking criterion
Program behaviour (4 points)
Each required behaviour works on the supplied cases.

Marking criterion
Use of the lesson concept (3 points)
The code uses the main idea in a clear, purposeful way.

Marking criterion
Test evidence (2 points)
The reflection describes a case, its expected result and what happened.

Marking criterion
Explanation (1 points)
The learner explains a change or design decision in their own words.

Stretch
Wrap a second function with the same with_timing helper. Give that run a fresh fake clock with two readings and explain why the first clock has no readings left.

Reflect
Which test would still fail if the timer worked but the original calculation bug remained?

Check your prediction
MISMATCH The hand-checked plan expects 8 minutes. The program reports 3, so the numbers differ.
