F03 · PREDICT · DEBUG · VERIFY LOCALLY
F03 Stable IDs: Why Did Deletion Target the Wrong Record?
Remember that the second item is the algorithms document. Remove the first item or sort, and the old index points elsewhere. Position and identity differ.
Back to the practice route →The bug and the local contract
def remove_by_id(documents, document_id):
index = next((i for i, item in enumerate(documents)
if item["id"] == document_id), -1)
return documents.pop(index)
- Implement find_by_id(documents, document_id) and remove_by_id(documents, document_id) using linear search. Lookup preserves input; removal targets only that ID and preserves other items’ order.
- Report a miss explicitly without removing another record. Test first, middle and last items, missing and empty inputs, lookup after deletion and lookup by the original ID after sorting.
- IDs do not change with position and are not reused. C array access out of bounds is undefined behavior; Python negative indexing or a diagram is not a guaranteed C outcome.
Use fictional document metadata. The web model does not execute arbitrary code.
01 · CONTROLLED MODEL
Predict, then inspect each step
This models only the selected preset. It does not execute your Python draft or read/write computer files. One matching case does not establish correctness for all inputs.
Checks actual modeled IDs, content, order and the missing result. Negative-index removal describes Python, not guaranteed behavior for out-of-bounds C access.
Unchecked or invalidated
Before
{
"records": [
{
"id": "doc_00000000000000000000000000000001",
"title": "Zeta",
"category": "course",
"tags": [
"study"
]
},
{
"id": "doc_00000000000000000000000000000002",
"title": "Alpha",
"category": "course",
"tags": [
"study"
]
},
{
"id": "doc_00000000000000000000000000000003",
"title": "Beta",
"category": "course",
"tags": [
"study"
]
}
],
"positions": [
{
"index": 0,
"id": "doc_00000000000000000000000000000001"
},
{
"index": 1,
"id": "doc_00000000000000000000000000000002"
},
{
"index": 2,
"id": "doc_00000000000000000000000000000003"
}
]
}Model result
Step through the trace to reveal the result
Progressive learning hints
Hints are fixed teaching content. Their use is recorded.
Hint 1
Draw position → ID. After deleting the first item, which ID occupies the saved position, and where did the original ID go?
Hint 2
Find the current ID instead of retaining a long-lived index. Return on a miss; -1 is not a safe deletion position.
Hint 3
Remove once after finding the ID. Assert remaining content and order, not only length; use the same ID before and after sorting.
My Python draft
Save and export your draft here. Code is not executed or synced to the starter. Draft, input or strategy edits invalidate earlier model results and self-reports.
Local learning records
Saved only in this browser; export is available. No cross-device sync or proof of mastery. Earlier F01 and six-task records are preserved through migration.
- F03 opened: —
- Model check: Unchecked or invalidated
- Local tests (self-reported): —
- Highest hint level: 0/3
- Reference viewed: —
- Unaided variant (self-reported): —
02 · Repair your local project
Start any task independently: its package contains that task’s bug and a runnable baseline for the other business functions. Python 3.11+, standard library only. See the package’s verification notes for tested environments.
- Extract into a new directory. Open a terminal and identify the file being run. Run public tests to reproduce the bug; consult the package’s intentional-failure list.
- Write a hypothesis and choose a value to inspect. Repair the specified function, retain public tests and record the diff and a minimal counterexample.
- Run this task’s tests, then the full regression suite. Web-model results do not replace local execution; self-reports are not remotely verified by this site.
python check_environment.py python -m unittest tests.test_f03 -v python -m unittest discover -s tests -v
03 · Independent variant
Locate the originally selected contact after sorting names. Independently implement find_contact(contacts, identifier) in challenges.py using a stable ID. Test same-name contacts, a missing person and an empty list. Explain why a retained index cannot identify someone over time.
python -m unittest tests.test_variants -v
Implement from the requirement, replace the skipped placeholder in tests/test_student_counterexamples.py and add an assertion that exposes the original bug. Explain the failure. Hint or reference use excludes an unaided completion record for this attempt.
Reference explanation and answer
Try your own repair first. Opening or downloading the reference records answer use. Passing reference tests establishes only that the materials run.
View reference explanation (records answer use)
Indices describe current order; IDs describe business identity. Deletion and sorting change positions, requiring a fresh ID lookup. Handle a miss before mutation: Python pop(-1) removes the last item. D03 optimizes lookup without changing the rule.
Download this task’s reference (answer) · Version and source digest
Course references and review
- C: day 31 arrays; day 41 pointers
Choose numbered lessons in the course. Bounds and identity are different concerns.
- Sorting algorithms
Optional: observe how sorting changes positions.