TactivergeLite

Lite / Intelligent Robotic Systems / Week 9

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 · Full interactive version

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.

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?

  1. About the same
  2. Roughly thousands of times more comparisons
  3. Slightly faster, because lists are simpler
Guess first, then reveal

Thousands of times more. Each membership test scans a list that grows to 10,000 cells, so total work is O(V²) ≈ 10⁸ comparisons instead of O(V) ≈ 4 × 10⁴ hash lookups. One changed line, a huge saving.

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.

What the interactive shows (text version)

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.

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

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

Intuitive

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.

Technical

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?'.

Applied

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

Practice

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

steps

comparisons

steps

comparisons

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?

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.

Reflect

Which idea from this term will change how you write code next term, and why?

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

← Project: measure the cost, plot it honestlyVectors: arrows you can add →