──────────────────────────────────────

32. Check the Library Shelves · Build a Shelf Check Report

Compare two lists and return a numbered report without changing the supplied records.

Lesson files · lessons/iteration-patterns/shelf-check-report

Before you start
- Use zip() after checking that two lists have the same length.
- Number items with enumerate() starting at 1.
- Use append() to build a list of results.

By the end
- Use enumerate() and zip() together to compare numbered pairs.
- Use a function to return a fresh list of report lines.
- Test the difference between an empty slot and an incomplete scan list.

Where you can use it
Compare an instrument inventory with the items found during a check. Number the positions and tell the reader which item is missing or different.

A librarian writes down which book code belongs in each slot, then scans the books in the same order. A dash marks an empty slot that the librarian checked. If the two lists have different lengths, the librarian needs to finish recording the slots before the program can compare them.

Check the lengths first. If they differ, return a one-item list straight away. Otherwise use zip() to make the pairs, then enumerate(pairs, start=1) to number them. In the loop, number holds the slot number and (expected, scanned) gives each side of the pair a name.

Build the report in a fresh list. Return it from the function and print its lines in the calling code. The original lists stay available for another report.

Predict
What does expected hold on the second trip through the loop?
- CP
- BK

Worked example (example.py)

expected_codes = ["AJ", "BK"]
scanned_codes = ["AJ", "CP"]
pairs = zip(expected_codes, scanned_codes)
for number, (expected, scanned) in enumerate(pairs, start=1):
    print(f"Slot {number} - {expected} / {scanned}")


Example result
Slot 1 - AJ / AJ
Slot 2 - BK / CP

Enter the expected book codes on the first line and the scans on the second. Separate entries with spaces. A dash marks a checked slot with no book.

Complete shelf_report(expected_codes, scanned_codes). If the lengths differ, return a list containing only Counts differ. For equal lengths, add one report line per slot. Use Slot 1 - OK for a match, Slot 2 - Missing for a dash, and Slot 3 - Expected CR, found DX for a mismatch. Use the actual slot number and codes in each line.

Print the returned lines outside the function.

Starter program (starter.py)

def shelf_report(expected_codes, scanned_codes):
    # Build the report here.
    return ["Scan received"]

expected_codes = input().split()
scanned_codes = input().split()
for line in shelf_report(expected_codes, scanned_codes):
    print(line)


Starter input
AT BR CR
AT - DX

Hint
Start with the length check. For each numbered pair, decide whether it is missing, correct or mismatched. Append one line to a new report list.

Case 1. Try the sample
Input
AT BR CR
AT - DX
Expected output
Slot 1 - OK
Slot 2 - Missing
Slot 3 - Expected CR, found DX

Case 2. All books in place
Input
CR AT
CR AT
Expected output
Slot 1 - OK
Slot 2 - OK

Case 3. Missing scan record
Input
AT BR
AT
Expected output
Counts differ

Project checklist
Run the sample case.

Project checklist
Try one different case and explain what happened.

Marking criterion
Program behaviour (4 points)
Each required behaviour works on the supplied cases.

Marking criterion
Use of the lesson concept (3 points)
The code uses the main idea in a clear, purposeful way.

Marking criterion
Test evidence (2 points)
The reflection describes a case, its expected result and what happened.

Marking criterion
Explanation (1 points)
The learner explains a change or design decision in their own words.

Stretch
Call the function again with the same expected list and a corrected scan. Compare the two reports and confirm that the expected list still has its original entries.

Reflect
How does your report tell an empty slot apart from a scan that was never recorded?

Check your prediction
BK zip() pairs by position. The first list supplies expected and the second list supplies scanned.
