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
1.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.
- ◇ Combine modules, a class, lists and dictionaries in one small, tested program.
- ◇ Compare two data sets (recorded and counted stock) and report differences.
- ◇ Explain each design choice in a short written note.
2.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.
Can't use the interactive? Read a description instead
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).
3.Checkpoint: guided practice
Check what you just discovered before going on. Hints appear after a wrong answer.
Practice 1
recorded has keys {A, B, C}; counted has keys {B, C, D}. What is recorded.keys() - counted.keys()?
Practice 2
In the worked example, how many M3 bolts are missing from the shelf compared with the record?
4.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.
5.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
It's a spot-the-difference puzzle between two lists. Sorting both sides into labelled boxes (dicts) makes the differences jump out.
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.
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
- Nested loops are the only way to compare two collections.
✓ Sets and dictionaries answer 'which keys' and 'how many' questions directly and much faster. - If the program runs, the module check is done.
✓ The deliverable includes tests and a short note explaining the design choices.
6.Practice on your own
Practice, not a test. Wrong answers get a hint and another try.
Practice 3
Which structure makes audit() easiest to test?
Practice 4 · a fresh version every time
A log of motor temperature (°C) reads xs = [63, 66, 59, 62, 60, 54, 60]. What is the median, statistics.median(xs)? Give 2 decimal places.
Practice 5 · a fresh version every time
A wheel encoder log is readings = [45, 5, 63, 55, 46, 46, 16, 12, 19, 44]. What does readings[2] evaluate to?
Practice 6
Which report line is most useful to the lab manager?
7.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?
Guess first. Being wrong here is how the surprise works.
8.Reflect
9.Go further
- Python docs: set types ↗Set operations (difference, intersection, union) and how dictionary key views support them.
Concepts: Lists, dictionaries and tabular data · Classes and objects · Testing and debugging