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.
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.
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?
- Loop over both dicts comparing every pair
- Set difference: recorded.keys() - counted.keys()
- 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).
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
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.
Practice
Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.
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
- Python docs: set types
Set operations (difference, intersection, union) and how dictionary key views support them.