Term assessment: engineering computation, end to end
A teammate's planner is correct, tested and painfully slow, and its chart says it's fast. Can you find every problem in fifteen minutes?
About 45 min · 3 goals · Checked by automated tests
1.Why should I care?
Real engineering rarely asks one kind of question at a time. This review mixes the whole term: modules and objects, data structures, search and sort, growth rates, numerical methods and honest plots. Treat it as a code review of a teammate's work.
- ◇ Review a small program for structure, correctness, efficiency and honest reporting.
- ◇ Combine operation counting, search, numerical methods and statistics in mixed problems.
- ◇ Explain each fix with a reason grounded in this term's ideas.
2.Predict
A planner stores visited cells in a list and calls 'cell in visited' for every neighbour. On a 100 × 100 open map, roughly how does that compare with using a set?
Guess first. Being wrong here is how the surprise works.
3.Understand
Review in layers
Structure: one job per function, clear modules, no hidden globals. Correctness: tests with hand-worked answers and edge cases. Efficiency: count the hot loop and check the containers. Reporting: units, n, spread, honest axes and stated limits.
Look for hidden loops
x in some_list, list.pop(0), list.index(x) and string concatenation in a loop all hide a scan. Inside another loop they turn O(n) into O(n²). A set, a deque, a dict or "".join usually fixes it.
Numbers need context
A result without units, sample size, spread or method is not yet evidence. A good review asks: how was this computed, how uncertain is it, and does the chart show it fairly?
Explain it another way
Like a pit-stop check: tyres (structure), brakes (correctness), engine (efficiency), dashboard (reporting). Skip one and the car may still drive, until it doesn't.
Code review checklists catch more defects when they are specific: 'Is every membership test on a set or dict?', 'Does every function with a precondition check it?', 'Does every plot have labelled units?'.
Most engineering teams require at least one reviewer to approve a change before it is merged, and reviewers routinely check performance assumptions and charts as well as logic.
Worked example 1
Review this function. What's wrong, and what does the fixed version look like?
# BEFORE: works, but slow and its report misleads
def planner_report_before(times_ms):
seen = []
for t in times_ms:
if t not in seen: # hidden scan inside the loop: O(n^2)
seen.append(t)
avg = sum(times_ms) / len(times_ms) # crashes on an empty list
return "Planner is fast: " + str(int(avg)) # no unit, no spread, no n
# AFTER
import statistics as st
def planner_report(times_ms):
"""Summarise planning times in ms. Raises ValueError if there are none."""
if not times_ms:
raise ValueError("no timings to report")
distinct = len(set(times_ms)) # O(n) on average
spread = st.stdev(times_ms) if len(times_ms) > 1 else 0.0
return (f"median {st.median(times_ms):.1f} ms, s.d. {spread:.1f} ms, "
f"n = {len(times_ms)} runs ({distinct} distinct values)")
print(planner_report_before([12.0, 15.5, 12.0, 14.1]))
print(planner_report([12.0, 15.5, 12.0, 14.1]))
# Planner is fast: 13
# median 13.1 ms, s.d. 1.7 ms, n = 4 runs (3 distinct values)Common mix-ups
- If the tests pass, the code is finished.
✓ It's correct for those tests. Review structure, efficiency and reporting too. - Efficiency only matters for huge programs.
✓ A single hidden O(n²) line can make a small planner unusable on a realistic map.
4.Discover
In the Code Lab, review the worked example's planner_report function. List every problem you find under four headings: structure, correctness, efficiency, reporting. Fix them one at a time, re-running the tests after each fix.
Can't use the interactive? Read a description instead
The Code Lab review can be done on paper: the 'before' function uses a list membership test inside a loop (O(n²)), divides by zero on an empty list and reports a number without unit, spread or sample size. The fixed version uses a set, checks for empty input, and reports median, standard deviation and n in milliseconds.
5.Checkpoint: guided practice
Check what you just discovered before going on. Hints appear after a wrong answer.
Practice 1 · a fresh version every time
A program is comparing every pair of waypoints. Its step count is measured with: count = sum(1 for i in range(0, 266, 10)). What value does count hold?
6.Practice on your own
Practice, not a test. Wrong answers get a hint and another try.
Practice 2 · a fresh version every time
A sorted list holds 8 waypoint ids. Binary search compares the target with one middle element per loop and halves the range each time. In the worst case (including a target that is not there), how many elements does it compare with the target?
Practice 3 · a fresh version every time
Bisection starts from the bracket [1, 3] and stops once the bracket width is at most 0.001. What is the smallest number of steps that guarantees this?
Practice 4 · a fresh version every time
Part masses (g) arrive as a = [58, 46, 38, 32, 28]. They are sorted with insertion sort, where each new key moves left while j >= 0 and a[j] > key. Counting every evaluation of a[j] > key, how many comparisons are made?
Practice 5 · a fresh version every time
A log of motor temperature (°C) reads xs = [42, 50, 49, 47, 47, 44, 43, 44, 47]. What is the mean, statistics.mean(xs)? Give 2 decimal places.
Practice 6
Which line inside a loop over n items makes the whole loop O(n²)?
Practice 7
You need a root of a smooth f on [1, 2] (with a sign change) to 10⁻¹² quickly and safely. Best approach?
7.Apply it
Term assessment. Hand in your finished term project (planner package, tests, measurements, two plots, interpretation) together with a one-page review of a classmate's project (or the worked example) under the four headings. Assessed with the term rubric: correctness, software structure, efficiency analysis, visual communication.
8.Reflect
Concepts: Algorithms, searching and complexity · Testing and debugging · Root finding and iterative methods · Honest data visualisation