TactivergeLite

Lite / Intelligent Robotic Systems / Week 3

Module check: the stores audit

The robotics lab's stores list says 120 M3 bolts. The shelf has 96. Your job: write the program that finds every mismatch before the next build day.

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

Why should I care?

This module check brings weeks 1–3 together on a task real labs do: reconcile what the records say with what is actually on the shelf. It needs clean structure, careful data handling and honest reporting.

Predict

You have the recorded stock as a dict {part_no: qty} and the shelf count as another dict. What is the clearest way to find parts missing from the shelf count entirely?

  1. Loop over both dicts comparing every pair
  2. Set difference: recorded.keys() - counted.keys()
  3. Sort both and compare by index
Guess first, then reveal

Dictionary key views behave like sets, so recorded.keys() - counted.keys() gives every part on record that nobody counted. One line, no nested loops, and it reads exactly like the question.

Discover

In the Code Lab, build the audit: a stock.py module with a load function, a Part class (or plain dicts: justify your choice), and an audit(recorded, counted) function that returns missing, unexpected and mismatched parts. Run it on the worked example's data, then add three tests.

What the interactive shows (text version)

The Code Lab activity can be done on paper: recorded stock {M3-BOLT 120, M3-NUT 340, BRG-608 16, SERVO-9G 6} and shelf count {M3-BOLT 96, M3-NUT 340, BRG-608 14, BELT-GT2 3}. Missing: SERVO-9G. Unexpected: BELT-GT2. Mismatched: M3-BOLT (24 short), BRG-608 (2 short).

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

Understand

Plan the pieces first

Loading (text → records), comparing (two dicts → differences) and reporting (differences → readable text) are three jobs. Give each its own function, keep loading separate from comparing so you can test the comparison on hand-made dicts.

Sets for 'which keys', dicts for 'how many'

recorded.keys() - counted.keys() finds parts never counted; the reverse finds surprises on the shelf; recorded.keys() & counted.keys() gives parts in both, whose quantities you then compare.

Report like an engineer

Sort the output, give units, and lead with what matters: the biggest shortfalls first. Say how many rows were rejected when loading. A short note on design choices (why a dict, why a class or not) is part of the deliverable.

Explain it another way

Intuitive

It's a spot-the-difference puzzle between two lists. Sorting both sides into labelled boxes (dicts) makes the differences jump out.

Technical

Set operations on dict key views are O(len) on average, compared with O(n·m) for nested loops. Returning plain data (lists of tuples) from audit() keeps it testable; formatting happens in a separate report function.

Applied

Stock reconciliation is a standard task in workshops and factories; the same pattern (keys only in A, only in B, in both with different values) is used to compare configuration files, parts lists and sensor calibration tables.

Worked example 1

Audit recorded stock against a shelf count.

def audit(recorded, counted):
    """Compare {part_no: qty} dicts. Returns (missing, unexpected, mismatched) sorted lists."""
    missing = sorted(recorded.keys() - counted.keys())
    unexpected = sorted(counted.keys() - recorded.keys())
    mismatched = sorted(
        ((p, recorded[p], counted[p]) for p in recorded.keys() & counted.keys() if recorded[p] != counted[p]),
        key=lambda t: t[2] - t[1],          # biggest shortfall first
    )
    return missing, unexpected, mismatched


recorded = {"M3-BOLT": 120, "M3-NUT": 340, "BRG-608": 16, "SERVO-9G": 6}
counted = {"M3-BOLT": 96, "M3-NUT": 340, "BRG-608": 14, "BELT-GT2": 3}
missing, unexpected, mismatched = audit(recorded, counted)
print(missing)       # ['SERVO-9G']
print(unexpected)    # ['BELT-GT2']
print(mismatched)    # [('M3-BOLT', 120, 96), ('BRG-608', 16, 14)]

assert audit({}, {}) == ([], [], [])
assert audit({"A": 1}, {"A": 1}) == ([], [], [])

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 1. recorded has keys {A, B, C}; counted has keys {B, C, D}. What is recorded.keys() - counted.keys()?

bolts

Practice 3. Which structure makes audit() easiest to test?

Practice 6. Which report line is most useful to the lab manager?

Apply it

Module check deliverable. A small package with stock.py (loading, with a rejected-row count), audit.py (comparison) and main.py (report), at least six tests (including empty and identical inputs), and a 150-word note: why you chose dicts, whether a Part class helped, and what your report puts first.

Reflect

Which part of weeks 1–3 (modules, classes, lists, dicts) made this task easiest, and which do you need to practise?

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

Go further

← From CSV file to records you can queryList, tuple, set or dict? Pick the right container →