When a learner can reproduce a familiar example but a small variation causes confusion, the problem is usually an incomplete model rather than a lack of effort. The fastest useful response is to expose the hidden state and test one boundary at a time.
Python collections.deque is a double-ended queue whose append and pop operations at either end are thread-safe, memory-efficient and approximately O(1). Mastery means tracking left and right precisely, predicting eviction in a bounded deque, recognising that extendleft reverses the visible order of its input, separating fast end access from slower middle access, treating atomic methods as smaller than a complete concurrent protocol, and choosing deque only when the access pattern matches its contract. This guide begins with that mechanism, then develops it through worked traces, deliberate mistakes, explained practice and transfer decisions.
The aim is independent reasoning. A learner should be able to predict behaviour, locate the earliest wrong assumption, use a safe diagnostic procedure and defend a design choice in a new project.
Punggol families can use the guide in short sessions around homework, CCAs and rest. The activities are proposed learning exercises, not claims about a physical branch, timetable, class size, fee, school relationship or guaranteed result.
Use disposable data and repositories, preserve backups, and check version-sensitive details against the official source. Current documentation settles a technical contract; observation and explanation turn that contract into usable knowledge.
Find your next learning step
Choose the route that matches the present difficulty. Use the complete index for a systematic course.
Build the model
Chapters 1-4 . Begin here, then continue after the learner can predict, verify and explain.
Use the core tools
Chapters 5-8 . Begin here, then continue after the learner can predict, verify and explain.
Handle boundaries
Chapters 9-12 . Begin here, then continue after the learner can predict, verify and explain.
Debug and verify
Chapters 13-16 . Begin here, then continue after the learner can predict, verify and explain.
Transfer with judgment
Chapters 17-20 . Begin here, then continue after the learner can predict, verify and explain.
Open the full chapter index . Jump to capstone practice . Use the How Studying Works hub . Read the official documentation
Complete chapter index
Chapters 1-4 . Build the model
Chapters 5-8 . Use the core tools
Chapters 9-12 . Handle boundaries
Chapters 13-16 . Debug and verify
Chapters 17-20 . Transfer with judgment
deque(iterable) appends source items from left to right, so iteration initially matches the iterable. Treat that sentence as a testable model. A secure learner can point to the relevant input, name the operation, describe the resulting state and identify one observation that would prove the model incomplete.
The high-value mistake in this chapter is drawing the first source value on the right because the structure has two ends. It matters because the output may look reasonable while the ownership, ordering, identity or safety rule is wrong. State the contract for Construction preserves iterable order, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.
For the Construction preserves iterable order chapter on Python collections.deque, use a two-column trace during a short Punggol home session. On the left, write the predicted state for this exact mechanism before the tool runs. On the right, record the observation that bears on drawing the first source value on the right because the structure has two ends. Explain the earliest difference with one causal sentence, then repeat only the smallest changed case.
Core worked example
from collections import deque
d=deque(['A','B','C'])
print(list(d))Explained result. The visible order is A, B, C; A is leftmost and C is rightmost. Check the boundary as well as the happy path: ask what happens with an empty input, a duplicate or tied value, an unsupported type, a missing path, a NULL, or a second reference to the same object. Only the relevant boundary should be kept; the list is a prompt for judgment, not a demand to force every case into every example.
Four purposeful transfer cases
Case 1: homework queue. Predict the rule using new tasks arrive at the right and the next task leaves from the left. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “deque(iterable) appends source items from left to right, so iteration initially matches the iterable.” Apply this procedure: State the contract for Construction preserves iterable order, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The visible order is A, B, C; A is leftmost and C is rightmost. For the homework queue, add one near-miss that exposes drawing the first source value on the right because the structure has two ends. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Case 2: bus arrival window. Contrast the rule using only the latest five recorded intervals remain. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “deque(iterable) appends source items from left to right, so iteration initially matches the iterable.” Apply this procedure: State the contract for Construction preserves iterable order, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The visible order is A, B, C; A is leftmost and C is rightmost. For the bus arrival window, add one near-miss that exposes drawing the first source value on the right because the structure has two ends. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Case 3: revision carousel. Stress-test the rule using topics rotate without rebuilding a list. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “deque(iterable) appends source items from left to right, so iteration initially matches the iterable.” Apply this procedure: State the contract for Construction preserves iterable order, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The visible order is A, B, C; A is leftmost and C is rightmost. For the revision carousel, add one near-miss that exposes drawing the first source value on the right because the structure has two ends. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Case 4: browser history sketch. Explain the rule using visits move between left and right stacks. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “deque(iterable) appends source items from left to right, so iteration initially matches the iterable.” Apply this procedure: State the contract for Construction preserves iterable order, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The visible order is A, B, C; A is leftmost and C is rightmost. For the browser history sketch, add one near-miss that exposes drawing the first source value on the right because the structure has two ends. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Diagnostic route
- Model check: ask the learner to draw or list the exact rows, fields, references, paths or states involved.
- Boundary check: create the smallest input that triggers drawing the first source value on the right because the structure has two ends.
- Evidence check: separate a printed value from identity, ordering, ownership, type or repository state.
- Repair check: use the reversible procedure “State the contract for Construction preserves iterable order, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.” and record the first changed observation.
- Transfer check: repeat the rule in a second context and identify what remains invariant.
A parent does not need to know the final Python collections.deque syntax. For this chapter, useful prompts are: “What did you expect from Construction preserves iterable order?”, “Which state changed first?”, “What evidence tests that prediction?”, and “Can the case exposing drawing the first source value on the right because the structure has two ends be made smaller?” The learner, not the parent, should supply the technical explanation.
Practice with an explained answer
Question. Build a tiny CCA turn order with participants cycle while absent names are removed. Include one ordinary case, one boundary and one deliberate failure caused by drawing the first source value on the right because the structure has two ends. Predict each result before using a tool, then report the first point where observation differs from prediction.
Answer guide. A strong response starts with the rule: deque(iterable) appends source items from left to right, so iteration initially matches the iterable. It shows a trace, not only a final value. The ordinary case should demonstrate “The visible order is A, B, C; A is leftmost and C is rightmost.” The boundary must exercise the same mechanism at an edge, and the deliberate failure must be repaired with: State the contract for Construction preserves iterable order, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. Other data choices are valid when the evidence supports the same causal chain.
Decision and transfer
For Construction preserves iterable order, separate the documented Python collections.deque mechanism from the project policy. State exactly what the technical contract guarantees, then state the project choice about validation, ordering, ownership, performance or recovery. Test whether the same distinction survives one transfer case, and keep stateful experiments disposable and backed up.
Previous chapter . Contents . Next chapter
append(x) adds exactly one item to the right end. Treat that sentence as a testable model. A secure learner can point to the relevant input, name the operation, describe the resulting state and identify one observation that would prove the model incomplete.
The high-value mistake in this chapter is calling append a queue operation without naming which end changes. It matters because the output may look reasonable while the ownership, ordering, identity or safety rule is wrong. State the contract for append adds at the right, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.
For the append adds at the right chapter on Python collections.deque, use a two-column trace during a short Punggol home session. On the left, write the predicted state for this exact mechanism before the tool runs. On the right, record the observation that bears on calling append a queue operation without naming which end changes. Explain the earliest difference with one causal sentence, then repeat only the smallest changed case.
Core worked example
d=deque(['A','B']); d.append('C'); print(list(d))Explained result. The deque becomes A, B, C and the previous left end is unchanged. Check the boundary as well as the happy path: ask what happens with an empty input, a duplicate or tied value, an unsupported type, a missing path, a NULL, or a second reference to the same object. Only the relevant boundary should be kept; the list is a prompt for judgment, not a demand to force every case into every example.
Four purposeful transfer cases
Case 1: revision carousel. Contrast the rule using topics rotate without rebuilding a list. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “append(x) adds exactly one item to the right end.” Apply this procedure: State the contract for append adds at the right, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The deque becomes A, B, C and the previous left end is unchanged. For the revision carousel, add one near-miss that exposes calling append a queue operation without naming which end changes. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Case 2: browser history sketch. Stress-test the rule using visits move between left and right stacks. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “append(x) adds exactly one item to the right end.” Apply this procedure: State the contract for append adds at the right, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The deque becomes A, B, C and the previous left end is unchanged. For the browser history sketch, add one near-miss that exposes calling append a queue operation without naming which end changes. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Case 3: science sensor buffer. Explain the rule using a fixed window holds recent readings. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “append(x) adds exactly one item to the right end.” Apply this procedure: State the contract for append adds at the right, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The deque becomes A, B, C and the previous left end is unchanged. For the science sensor buffer, add one near-miss that exposes calling append a queue operation without naming which end changes. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Case 4: CCA turn order. Transfer the rule using participants cycle while absent names are removed. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “append(x) adds exactly one item to the right end.” Apply this procedure: State the contract for append adds at the right, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The deque becomes A, B, C and the previous left end is unchanged. For the CCA turn order, add one near-miss that exposes calling append a queue operation without naming which end changes. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Diagnostic route
- Model check: ask the learner to draw or list the exact rows, fields, references, paths or states involved.
- Boundary check: create the smallest input that triggers calling append a queue operation without naming which end changes.
- Evidence check: separate a printed value from identity, ordering, ownership, type or repository state.
- Repair check: use the reversible procedure “State the contract for append adds at the right, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.” and record the first changed observation.
- Transfer check: repeat the rule in a second context and identify what remains invariant.
A parent does not need to know the final Python collections.deque syntax. For this chapter, useful prompts are: “What did you expect from append adds at the right?”, “Which state changed first?”, “What evidence tests that prediction?”, and “Can the case exposing calling append a queue operation without naming which end changes be made smaller?” The learner, not the parent, should supply the technical explanation.
Practice with an explained answer
Question. Build a tiny test laboratory with empty, singleton, bounded and duplicate values expose every transition. Include one ordinary case, one boundary and one deliberate failure caused by calling append a queue operation without naming which end changes. Predict each result before using a tool, then report the first point where observation differs from prediction.
Answer guide. A strong response starts with the rule: append(x) adds exactly one item to the right end. It shows a trace, not only a final value. The ordinary case should demonstrate “The deque becomes A, B, C and the previous left end is unchanged.” The boundary must exercise the same mechanism at an edge, and the deliberate failure must be repaired with: State the contract for append adds at the right, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. Other data choices are valid when the evidence supports the same causal chain.
Decision and transfer
For append adds at the right, separate the documented Python collections.deque mechanism from the project policy. State exactly what the technical contract guarantees, then state the project choice about validation, ordering, ownership, performance or recovery. Test whether the same distinction survives one transfer case, and keep stateful experiments disposable and backed up.
Previous chapter . Contents . Next chapter
appendleft(x) adds one item before the current leftmost item. Treat that sentence as a testable model. A secure learner can point to the relevant input, name the operation, describe the resulting state and identify one observation that would prove the model incomplete.
The high-value mistake in this chapter is using insert(0,x) from list habit and hiding the intended end operation. It matters because the output may look reasonable while the ownership, ordering, identity or safety rule is wrong. State the contract for appendleft adds at the left, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.
For the appendleft adds at the left chapter on Python collections.deque, use a two-column trace during a short Punggol home session. On the left, write the predicted state for this exact mechanism before the tool runs. On the right, record the observation that bears on using insert(0,x) from list habit and hiding the intended end operation. Explain the earliest difference with one causal sentence, then repeat only the smallest changed case.
Core worked example
d=deque(['B','C']); d.appendleft('A'); print(list(d))Explained result. The deque becomes A, B, C with the new item at the left. Check the boundary as well as the happy path: ask what happens with an empty input, a duplicate or tied value, an unsupported type, a missing path, a NULL, or a second reference to the same object. Only the relevant boundary should be kept; the list is a prompt for judgment, not a demand to force every case into every example.
Four purposeful transfer cases
Case 1: science sensor buffer. Stress-test the rule using a fixed window holds recent readings. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “appendleft(x) adds one item before the current leftmost item.” Apply this procedure: State the contract for appendleft adds at the left, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The deque becomes A, B, C with the new item at the left. For the science sensor buffer, add one near-miss that exposes using insert(0,x) from list habit and hiding the intended end operation. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Case 2: CCA turn order. Explain the rule using participants cycle while absent names are removed. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “appendleft(x) adds one item before the current leftmost item.” Apply this procedure: State the contract for appendleft adds at the left, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The deque becomes A, B, C with the new item at the left. For the CCA turn order, add one near-miss that exposes using insert(0,x) from list habit and hiding the intended end operation. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Case 3: test laboratory. Transfer the rule using empty, singleton, bounded and duplicate values expose every transition. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “appendleft(x) adds one item before the current leftmost item.” Apply this procedure: State the contract for appendleft adds at the left, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The deque becomes A, B, C with the new item at the left. For the test laboratory, add one near-miss that exposes using insert(0,x) from list habit and hiding the intended end operation. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Case 4: design decision. Predict the rule using deque is compared with list, queue.Queue and heapq. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “appendleft(x) adds one item before the current leftmost item.” Apply this procedure: State the contract for appendleft adds at the left, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The deque becomes A, B, C with the new item at the left. For the design decision, add one near-miss that exposes using insert(0,x) from list habit and hiding the intended end operation. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Diagnostic route
- Model check: ask the learner to draw or list the exact rows, fields, references, paths or states involved.
- Boundary check: create the smallest input that triggers using insert(0,x) from list habit and hiding the intended end operation.
- Evidence check: separate a printed value from identity, ordering, ownership, type or repository state.
- Repair check: use the reversible procedure “State the contract for appendleft adds at the left, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.” and record the first changed observation.
- Transfer check: repeat the rule in a second context and identify what remains invariant.
A parent does not need to know the final Python collections.deque syntax. For this chapter, useful prompts are: “What did you expect from appendleft adds at the left?”, “Which state changed first?”, “What evidence tests that prediction?”, and “Can the case exposing using insert(0,x) from list habit and hiding the intended end operation be made smaller?” The learner, not the parent, should supply the technical explanation.
Practice with an explained answer
Question. Build a tiny design decision with deque is compared with list, queue.Queue and heapq. Include one ordinary case, one boundary and one deliberate failure caused by using insert(0,x) from list habit and hiding the intended end operation. Predict each result before using a tool, then report the first point where observation differs from prediction.
Answer guide. A strong response starts with the rule: appendleft(x) adds one item before the current leftmost item. It shows a trace, not only a final value. The ordinary case should demonstrate “The deque becomes A, B, C with the new item at the left.” The boundary must exercise the same mechanism at an edge, and the deliberate failure must be repaired with: State the contract for appendleft adds at the left, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. Other data choices are valid when the evidence supports the same causal chain.
Decision and transfer
For appendleft adds at the left, separate the documented Python collections.deque mechanism from the project policy. State exactly what the technical contract guarantees, then state the project choice about validation, ordering, ownership, performance or recovery. Test whether the same distinction survives one transfer case, and keep stateful experiments disposable and backed up.
Previous chapter . Contents . Next chapter
pop removes and returns the rightmost item, while popleft removes and returns the leftmost item. Treat that sentence as a testable model. A secure learner can point to the relevant input, name the operation, describe the resulting state and identify one observation that would prove the model incomplete.
The high-value mistake in this chapter is writing pop when the algorithm requires FIFO removal from the left. It matters because the output may look reasonable while the ownership, ordering, identity or safety rule is wrong. State the contract for pop and popleft remove different ends, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.
For the pop and popleft remove different ends chapter on Python collections.deque, use a two-column trace during a short Punggol home session. On the left, write the predicted state for this exact mechanism before the tool runs. On the right, record the observation that bears on writing pop when the algorithm requires FIFO removal from the left. Explain the earliest difference with one causal sentence, then repeat only the smallest changed case.
Core worked example
d=deque(['A','B','C']); print(d.popleft(),d.pop(),list(d))Explained result. The calls return A then C, leaving B. Check the boundary as well as the happy path: ask what happens with an empty input, a duplicate or tied value, an unsupported type, a missing path, a NULL, or a second reference to the same object. Only the relevant boundary should be kept; the list is a prompt for judgment, not a demand to force every case into every example.
Four purposeful transfer cases
Case 1: test laboratory. Explain the rule using empty, singleton, bounded and duplicate values expose every transition. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “pop removes and returns the rightmost item, while popleft removes and returns the leftmost item.” Apply this procedure: State the contract for pop and popleft remove different ends, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The calls return A then C, leaving B. For the test laboratory, add one near-miss that exposes writing pop when the algorithm requires FIFO removal from the left. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Case 2: design decision. Transfer the rule using deque is compared with list, queue.Queue and heapq. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “pop removes and returns the rightmost item, while popleft removes and returns the leftmost item.” Apply this procedure: State the contract for pop and popleft remove different ends, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The calls return A then C, leaving B. For the design decision, add one near-miss that exposes writing pop when the algorithm requires FIFO removal from the left. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Case 3: homework queue. Predict the rule using new tasks arrive at the right and the next task leaves from the left. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “pop removes and returns the rightmost item, while popleft removes and returns the leftmost item.” Apply this procedure: State the contract for pop and popleft remove different ends, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The calls return A then C, leaving B. For the homework queue, add one near-miss that exposes writing pop when the algorithm requires FIFO removal from the left. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Case 4: bus arrival window. Contrast the rule using only the latest five recorded intervals remain. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “pop removes and returns the rightmost item, while popleft removes and returns the leftmost item.” Apply this procedure: State the contract for pop and popleft remove different ends, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The calls return A then C, leaving B. For the bus arrival window, add one near-miss that exposes writing pop when the algorithm requires FIFO removal from the left. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Diagnostic route
- Model check: ask the learner to draw or list the exact rows, fields, references, paths or states involved.
- Boundary check: create the smallest input that triggers writing pop when the algorithm requires FIFO removal from the left.
- Evidence check: separate a printed value from identity, ordering, ownership, type or repository state.
- Repair check: use the reversible procedure “State the contract for pop and popleft remove different ends, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.” and record the first changed observation.
- Transfer check: repeat the rule in a second context and identify what remains invariant.
A parent does not need to know the final Python collections.deque syntax. For this chapter, useful prompts are: “What did you expect from pop and popleft remove different ends?”, “Which state changed first?”, “What evidence tests that prediction?”, and “Can the case exposing writing pop when the algorithm requires FIFO removal from the left be made smaller?” The learner, not the parent, should supply the technical explanation.
Practice with an explained answer
Question. Build a tiny homework queue with new tasks arrive at the right and the next task leaves from the left. Include one ordinary case, one boundary and one deliberate failure caused by writing pop when the algorithm requires FIFO removal from the left. Predict each result before using a tool, then report the first point where observation differs from prediction.
Answer guide. A strong response starts with the rule: pop removes and returns the rightmost item, while popleft removes and returns the leftmost item. It shows a trace, not only a final value. The ordinary case should demonstrate “The calls return A then C, leaving B.” The boundary must exercise the same mechanism at an edge, and the deliberate failure must be repaired with: State the contract for pop and popleft remove different ends, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. Other data choices are valid when the evidence supports the same causal chain.
Decision and transfer
For pop and popleft remove different ends, separate the documented Python collections.deque mechanism from the project policy. State exactly what the technical contract guarantees, then state the project choice about validation, ordering, ownership, performance or recovery. Test whether the same distinction survives one transfer case, and keep stateful experiments disposable and backed up.
Previous chapter . Contents . Next chapter
pop and popleft raise IndexError when the deque is empty rather than returning a sentinel. Treat that sentence as a testable model. A secure learner can point to the relevant input, name the operation, describe the resulting state and identify one observation that would prove the model incomplete.
The high-value mistake in this chapter is testing a truthy result after removal instead of deciding an empty policy. It matters because the output may look reasonable while the ownership, ordering, identity or safety rule is wrong. State the contract for Empty removal raises IndexError, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.
For the Empty removal raises IndexError chapter on Python collections.deque, use a two-column trace during a short Punggol home session. On the left, write the predicted state for this exact mechanism before the tool runs. On the right, record the observation that bears on testing a truthy result after removal instead of deciding an empty policy. Explain the earliest difference with one causal sentence, then repeat only the smallest changed case.
Core worked example
d=deque()
try: d.popleft()
except IndexError: print('empty')Explained result. The exception exposes the empty boundary, so the caller must guard or handle it deliberately. Check the boundary as well as the happy path: ask what happens with an empty input, a duplicate or tied value, an unsupported type, a missing path, a NULL, or a second reference to the same object. Only the relevant boundary should be kept; the list is a prompt for judgment, not a demand to force every case into every example.
Four purposeful transfer cases
Case 1: homework queue. Transfer the rule using new tasks arrive at the right and the next task leaves from the left. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “pop and popleft raise IndexError when the deque is empty rather than returning a sentinel.” Apply this procedure: State the contract for Empty removal raises IndexError, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The exception exposes the empty boundary, so the caller must guard or handle it deliberately. For the homework queue, add one near-miss that exposes testing a truthy result after removal instead of deciding an empty policy. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Case 2: bus arrival window. Predict the rule using only the latest five recorded intervals remain. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “pop and popleft raise IndexError when the deque is empty rather than returning a sentinel.” Apply this procedure: State the contract for Empty removal raises IndexError, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The exception exposes the empty boundary, so the caller must guard or handle it deliberately. For the bus arrival window, add one near-miss that exposes testing a truthy result after removal instead of deciding an empty policy. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Case 3: revision carousel. Contrast the rule using topics rotate without rebuilding a list. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “pop and popleft raise IndexError when the deque is empty rather than returning a sentinel.” Apply this procedure: State the contract for Empty removal raises IndexError, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The exception exposes the empty boundary, so the caller must guard or handle it deliberately. For the revision carousel, add one near-miss that exposes testing a truthy result after removal instead of deciding an empty policy. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Case 4: browser history sketch. Stress-test the rule using visits move between left and right stacks. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “pop and popleft raise IndexError when the deque is empty rather than returning a sentinel.” Apply this procedure: State the contract for Empty removal raises IndexError, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The exception exposes the empty boundary, so the caller must guard or handle it deliberately. For the browser history sketch, add one near-miss that exposes testing a truthy result after removal instead of deciding an empty policy. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Diagnostic route
- Model check: ask the learner to draw or list the exact rows, fields, references, paths or states involved.
- Boundary check: create the smallest input that triggers testing a truthy result after removal instead of deciding an empty policy.
- Evidence check: separate a printed value from identity, ordering, ownership, type or repository state.
- Repair check: use the reversible procedure “State the contract for Empty removal raises IndexError, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.” and record the first changed observation.
- Transfer check: repeat the rule in a second context and identify what remains invariant.
A parent does not need to know the final Python collections.deque syntax. For this chapter, useful prompts are: “What did you expect from Empty removal raises IndexError?”, “Which state changed first?”, “What evidence tests that prediction?”, and “Can the case exposing testing a truthy result after removal instead of deciding an empty policy be made smaller?” The learner, not the parent, should supply the technical explanation.
Practice with an explained answer
Question. Build a tiny bus arrival window with only the latest five recorded intervals remain. Include one ordinary case, one boundary and one deliberate failure caused by testing a truthy result after removal instead of deciding an empty policy. Predict each result before using a tool, then report the first point where observation differs from prediction.
Answer guide. A strong response starts with the rule: pop and popleft raise IndexError when the deque is empty rather than returning a sentinel. It shows a trace, not only a final value. The ordinary case should demonstrate “The exception exposes the empty boundary, so the caller must guard or handle it deliberately.” The boundary must exercise the same mechanism at an edge, and the deliberate failure must be repaired with: State the contract for Empty removal raises IndexError, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. Other data choices are valid when the evidence supports the same causal chain.
Decision and transfer
For Empty removal raises IndexError, separate the documented Python collections.deque mechanism from the project policy. State exactly what the technical contract guarantees, then state the project choice about validation, ordering, ownership, performance or recovery. Test whether the same distinction survives one transfer case, and keep stateful experiments disposable and backed up.
Previous chapter . Contents . Next chapter
Deque appends and pops at either end are approximately O(1), while list pop(0) and insert(0,x) require O(n) movement. Treat that sentence as a testable model. A secure learner can point to the relevant input, name the operation, describe the resulting state and identify one observation that would prove the model incomplete.
The high-value mistake in this chapter is claiming deque is universally faster without checking the actual access pattern. It matters because the output may look reasonable while the ownership, ordering, identity or safety rule is wrong. State the contract for End operations avoid list front shifting, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.
For the End operations avoid list front shifting chapter on Python collections.deque, use a two-column trace during a short Punggol home session. On the left, write the predicted state for this exact mechanism before the tool runs. On the right, record the observation that bears on claiming deque is universally faster without checking the actual access pattern. Explain the earliest difference with one causal sentence, then repeat only the smallest changed case.
Core worked example
d=deque(range(5)); first=d.popleft(); d.append(5)Explained result. The queue-shaped update removes the left end and adds the right end without shifting every remaining element. Check the boundary as well as the happy path: ask what happens with an empty input, a duplicate or tied value, an unsupported type, a missing path, a NULL, or a second reference to the same object. Only the relevant boundary should be kept; the list is a prompt for judgment, not a demand to force every case into every example.
Four purposeful transfer cases
Case 1: revision carousel. Predict the rule using topics rotate without rebuilding a list. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “Deque appends and pops at either end are approximately O(1), while list pop(0) and insert(0,x) require O(n) movement.” Apply this procedure: State the contract for End operations avoid list front shifting, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The queue-shaped update removes the left end and adds the right end without shifting every remaining element. For the revision carousel, add one near-miss that exposes claiming deque is universally faster without checking the actual access pattern. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Case 2: browser history sketch. Contrast the rule using visits move between left and right stacks. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “Deque appends and pops at either end are approximately O(1), while list pop(0) and insert(0,x) require O(n) movement.” Apply this procedure: State the contract for End operations avoid list front shifting, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The queue-shaped update removes the left end and adds the right end without shifting every remaining element. For the browser history sketch, add one near-miss that exposes claiming deque is universally faster without checking the actual access pattern. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Case 3: science sensor buffer. Stress-test the rule using a fixed window holds recent readings. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “Deque appends and pops at either end are approximately O(1), while list pop(0) and insert(0,x) require O(n) movement.” Apply this procedure: State the contract for End operations avoid list front shifting, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The queue-shaped update removes the left end and adds the right end without shifting every remaining element. For the science sensor buffer, add one near-miss that exposes claiming deque is universally faster without checking the actual access pattern. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Case 4: CCA turn order. Explain the rule using participants cycle while absent names are removed. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “Deque appends and pops at either end are approximately O(1), while list pop(0) and insert(0,x) require O(n) movement.” Apply this procedure: State the contract for End operations avoid list front shifting, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The queue-shaped update removes the left end and adds the right end without shifting every remaining element. For the CCA turn order, add one near-miss that exposes claiming deque is universally faster without checking the actual access pattern. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Diagnostic route
- Model check: ask the learner to draw or list the exact rows, fields, references, paths or states involved.
- Boundary check: create the smallest input that triggers claiming deque is universally faster without checking the actual access pattern.
- Evidence check: separate a printed value from identity, ordering, ownership, type or repository state.
- Repair check: use the reversible procedure “State the contract for End operations avoid list front shifting, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.” and record the first changed observation.
- Transfer check: repeat the rule in a second context and identify what remains invariant.
A parent does not need to know the final Python collections.deque syntax. For this chapter, useful prompts are: “What did you expect from End operations avoid list front shifting?”, “Which state changed first?”, “What evidence tests that prediction?”, and “Can the case exposing claiming deque is universally faster without checking the actual access pattern be made smaller?” The learner, not the parent, should supply the technical explanation.
Practice with an explained answer
Question. Build a tiny revision carousel with topics rotate without rebuilding a list. Include one ordinary case, one boundary and one deliberate failure caused by claiming deque is universally faster without checking the actual access pattern. Predict each result before using a tool, then report the first point where observation differs from prediction.
Answer guide. A strong response starts with the rule: Deque appends and pops at either end are approximately O(1), while list pop(0) and insert(0,x) require O(n) movement. It shows a trace, not only a final value. The ordinary case should demonstrate “The queue-shaped update removes the left end and adds the right end without shifting every remaining element.” The boundary must exercise the same mechanism at an edge, and the deliberate failure must be repaired with: State the contract for End operations avoid list front shifting, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. Other data choices are valid when the evidence supports the same causal chain.
Decision and transfer
For End operations avoid list front shifting, separate the documented Python collections.deque mechanism from the project policy. State exactly what the technical contract guarantees, then state the project choice about validation, ordering, ownership, performance or recovery. Test whether the same distinction survives one transfer case, and keep stateful experiments disposable and backed up.
Previous chapter . Contents . Next chapter
A non-None maxlen fixes the capacity and makes new additions discard items from the opposite end when full. Treat that sentence as a testable model. A secure learner can point to the relevant input, name the operation, describe the resulting state and identify one observation that would prove the model incomplete.
The high-value mistake in this chapter is treating maxlen as a warning threshold that allows temporary overflow. It matters because the output may look reasonable while the ownership, ordering, identity or safety rule is wrong. State the contract for maxlen creates a bounded deque, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.
For the maxlen creates a bounded deque chapter on Python collections.deque, use a two-column trace during a short Punggol home session. On the left, write the predicted state for this exact mechanism before the tool runs. On the right, record the observation that bears on treating maxlen as a warning threshold that allows temporary overflow. Explain the earliest difference with one causal sentence, then repeat only the smallest changed case.
Core worked example
d=deque([1,2,3],maxlen=3); d.append(4); print(list(d))Explained result. Appending at the right discards 1 from the left, leaving 2, 3, 4. Check the boundary as well as the happy path: ask what happens with an empty input, a duplicate or tied value, an unsupported type, a missing path, a NULL, or a second reference to the same object. Only the relevant boundary should be kept; the list is a prompt for judgment, not a demand to force every case into every example.
Four purposeful transfer cases
Case 1: science sensor buffer. Contrast the rule using a fixed window holds recent readings. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “A non-None maxlen fixes the capacity and makes new additions discard items from the opposite end when full.” Apply this procedure: State the contract for maxlen creates a bounded deque, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: Appending at the right discards 1 from the left, leaving 2, 3, 4. For the science sensor buffer, add one near-miss that exposes treating maxlen as a warning threshold that allows temporary overflow. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Case 2: CCA turn order. Stress-test the rule using participants cycle while absent names are removed. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “A non-None maxlen fixes the capacity and makes new additions discard items from the opposite end when full.” Apply this procedure: State the contract for maxlen creates a bounded deque, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: Appending at the right discards 1 from the left, leaving 2, 3, 4. For the CCA turn order, add one near-miss that exposes treating maxlen as a warning threshold that allows temporary overflow. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Case 3: test laboratory. Explain the rule using empty, singleton, bounded and duplicate values expose every transition. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “A non-None maxlen fixes the capacity and makes new additions discard items from the opposite end when full.” Apply this procedure: State the contract for maxlen creates a bounded deque, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: Appending at the right discards 1 from the left, leaving 2, 3, 4. For the test laboratory, add one near-miss that exposes treating maxlen as a warning threshold that allows temporary overflow. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Case 4: design decision. Transfer the rule using deque is compared with list, queue.Queue and heapq. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “A non-None maxlen fixes the capacity and makes new additions discard items from the opposite end when full.” Apply this procedure: State the contract for maxlen creates a bounded deque, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: Appending at the right discards 1 from the left, leaving 2, 3, 4. For the design decision, add one near-miss that exposes treating maxlen as a warning threshold that allows temporary overflow. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Diagnostic route
- Model check: ask the learner to draw or list the exact rows, fields, references, paths or states involved.
- Boundary check: create the smallest input that triggers treating maxlen as a warning threshold that allows temporary overflow.
- Evidence check: separate a printed value from identity, ordering, ownership, type or repository state.
- Repair check: use the reversible procedure “State the contract for maxlen creates a bounded deque, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.” and record the first changed observation.
- Transfer check: repeat the rule in a second context and identify what remains invariant.
A parent does not need to know the final Python collections.deque syntax. For this chapter, useful prompts are: “What did you expect from maxlen creates a bounded deque?”, “Which state changed first?”, “What evidence tests that prediction?”, and “Can the case exposing treating maxlen as a warning threshold that allows temporary overflow be made smaller?” The learner, not the parent, should supply the technical explanation.
Practice with an explained answer
Question. Build a tiny browser history sketch with visits move between left and right stacks. Include one ordinary case, one boundary and one deliberate failure caused by treating maxlen as a warning threshold that allows temporary overflow. Predict each result before using a tool, then report the first point where observation differs from prediction.
Answer guide. A strong response starts with the rule: A non-None maxlen fixes the capacity and makes new additions discard items from the opposite end when full. It shows a trace, not only a final value. The ordinary case should demonstrate “Appending at the right discards 1 from the left, leaving 2, 3, 4.” The boundary must exercise the same mechanism at an edge, and the deliberate failure must be repaired with: State the contract for maxlen creates a bounded deque, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. Other data choices are valid when the evidence supports the same causal chain.
Decision and transfer
For maxlen creates a bounded deque, separate the documented Python collections.deque mechanism from the project policy. State exactly what the technical contract guarantees, then state the project choice about validation, ordering, ownership, performance or recovery. Test whether the same distinction survives one transfer case, and keep stateful experiments disposable and backed up.
Previous chapter . Contents . Next chapter
In a full bounded deque, appendleft adds on the left and discards one item from the right. Treat that sentence as a testable model. A secure learner can point to the relevant input, name the operation, describe the resulting state and identify one observation that would prove the model incomplete.
The high-value mistake in this chapter is memorising eviction as always removing the oldest item without defining orientation. It matters because the output may look reasonable while the ownership, ordering, identity or safety rule is wrong. State the contract for appendleft evicts from the right when full, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.
For the appendleft evicts from the right when full chapter on Python collections.deque, use a two-column trace during a short Punggol home session. On the left, write the predicted state for this exact mechanism before the tool runs. On the right, record the observation that bears on memorising eviction as always removing the oldest item without defining orientation. Explain the earliest difference with one causal sentence, then repeat only the smallest changed case.
Core worked example
d=deque([1,2,3],maxlen=3); d.appendleft(0); print(list(d))Explained result. The deque becomes 0, 1, 2 and the rightmost 3 is discarded. Check the boundary as well as the happy path: ask what happens with an empty input, a duplicate or tied value, an unsupported type, a missing path, a NULL, or a second reference to the same object. Only the relevant boundary should be kept; the list is a prompt for judgment, not a demand to force every case into every example.
Four purposeful transfer cases
Case 1: test laboratory. Stress-test the rule using empty, singleton, bounded and duplicate values expose every transition. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “In a full bounded deque, appendleft adds on the left and discards one item from the right.” Apply this procedure: State the contract for appendleft evicts from the right when full, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The deque becomes 0, 1, 2 and the rightmost 3 is discarded. For the test laboratory, add one near-miss that exposes memorising eviction as always removing the oldest item without defining orientation. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Case 2: design decision. Explain the rule using deque is compared with list, queue.Queue and heapq. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “In a full bounded deque, appendleft adds on the left and discards one item from the right.” Apply this procedure: State the contract for appendleft evicts from the right when full, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The deque becomes 0, 1, 2 and the rightmost 3 is discarded. For the design decision, add one near-miss that exposes memorising eviction as always removing the oldest item without defining orientation. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Case 3: homework queue. Transfer the rule using new tasks arrive at the right and the next task leaves from the left. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “In a full bounded deque, appendleft adds on the left and discards one item from the right.” Apply this procedure: State the contract for appendleft evicts from the right when full, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The deque becomes 0, 1, 2 and the rightmost 3 is discarded. For the homework queue, add one near-miss that exposes memorising eviction as always removing the oldest item without defining orientation. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Case 4: bus arrival window. Predict the rule using only the latest five recorded intervals remain. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “In a full bounded deque, appendleft adds on the left and discards one item from the right.” Apply this procedure: State the contract for appendleft evicts from the right when full, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The deque becomes 0, 1, 2 and the rightmost 3 is discarded. For the bus arrival window, add one near-miss that exposes memorising eviction as always removing the oldest item without defining orientation. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Diagnostic route
- Model check: ask the learner to draw or list the exact rows, fields, references, paths or states involved.
- Boundary check: create the smallest input that triggers memorising eviction as always removing the oldest item without defining orientation.
- Evidence check: separate a printed value from identity, ordering, ownership, type or repository state.
- Repair check: use the reversible procedure “State the contract for appendleft evicts from the right when full, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.” and record the first changed observation.
- Transfer check: repeat the rule in a second context and identify what remains invariant.
A parent does not need to know the final Python collections.deque syntax. For this chapter, useful prompts are: “What did you expect from appendleft evicts from the right when full?”, “Which state changed first?”, “What evidence tests that prediction?”, and “Can the case exposing memorising eviction as always removing the oldest item without defining orientation be made smaller?” The learner, not the parent, should supply the technical explanation.
Practice with an explained answer
Question. Build a tiny science sensor buffer with a fixed window holds recent readings. Include one ordinary case, one boundary and one deliberate failure caused by memorising eviction as always removing the oldest item without defining orientation. Predict each result before using a tool, then report the first point where observation differs from prediction.
Answer guide. A strong response starts with the rule: In a full bounded deque, appendleft adds on the left and discards one item from the right. It shows a trace, not only a final value. The ordinary case should demonstrate “The deque becomes 0, 1, 2 and the rightmost 3 is discarded.” The boundary must exercise the same mechanism at an edge, and the deliberate failure must be repaired with: State the contract for appendleft evicts from the right when full, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. Other data choices are valid when the evidence supports the same causal chain.
Decision and transfer
For appendleft evicts from the right when full, separate the documented Python collections.deque mechanism from the project policy. State exactly what the technical contract guarantees, then state the project choice about validation, ordering, ownership, performance or recovery. Test whether the same distinction survives one transfer case, and keep stateful experiments disposable and backed up.
Previous chapter . Contents . Next chapter
deque(maxlen=0) accepts additions but immediately discards them because its capacity is zero. Treat that sentence as a testable model. A secure learner can point to the relevant input, name the operation, describe the resulting state and identify one observation that would prove the model incomplete.
The high-value mistake in this chapter is using maxlen zero as though it meant unbounded. It matters because the output may look reasonable while the ownership, ordering, identity or safety rule is wrong. State the contract for A zero-length deque stores nothing, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.
For the A zero-length deque stores nothing chapter on Python collections.deque, use a two-column trace during a short Punggol home session. On the left, write the predicted state for this exact mechanism before the tool runs. On the right, record the observation that bears on using maxlen zero as though it meant unbounded. Explain the earliest difference with one causal sentence, then repeat only the smallest changed case.
Core worked example
d=deque(maxlen=0); d.append('x'); print(len(d),list(d))Explained result. The length remains zero and iteration is empty. Check the boundary as well as the happy path: ask what happens with an empty input, a duplicate or tied value, an unsupported type, a missing path, a NULL, or a second reference to the same object. Only the relevant boundary should be kept; the list is a prompt for judgment, not a demand to force every case into every example.
Four purposeful transfer cases
Case 1: homework queue. Explain the rule using new tasks arrive at the right and the next task leaves from the left. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “deque(maxlen=0) accepts additions but immediately discards them because its capacity is zero.” Apply this procedure: State the contract for A zero-length deque stores nothing, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The length remains zero and iteration is empty. For the homework queue, add one near-miss that exposes using maxlen zero as though it meant unbounded. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Case 2: bus arrival window. Transfer the rule using only the latest five recorded intervals remain. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “deque(maxlen=0) accepts additions but immediately discards them because its capacity is zero.” Apply this procedure: State the contract for A zero-length deque stores nothing, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The length remains zero and iteration is empty. For the bus arrival window, add one near-miss that exposes using maxlen zero as though it meant unbounded. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Case 3: revision carousel. Predict the rule using topics rotate without rebuilding a list. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “deque(maxlen=0) accepts additions but immediately discards them because its capacity is zero.” Apply this procedure: State the contract for A zero-length deque stores nothing, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The length remains zero and iteration is empty. For the revision carousel, add one near-miss that exposes using maxlen zero as though it meant unbounded. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Case 4: browser history sketch. Contrast the rule using visits move between left and right stacks. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “deque(maxlen=0) accepts additions but immediately discards them because its capacity is zero.” Apply this procedure: State the contract for A zero-length deque stores nothing, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The length remains zero and iteration is empty. For the browser history sketch, add one near-miss that exposes using maxlen zero as though it meant unbounded. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Diagnostic route
- Model check: ask the learner to draw or list the exact rows, fields, references, paths or states involved.
- Boundary check: create the smallest input that triggers using maxlen zero as though it meant unbounded.
- Evidence check: separate a printed value from identity, ordering, ownership, type or repository state.
- Repair check: use the reversible procedure “State the contract for A zero-length deque stores nothing, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.” and record the first changed observation.
- Transfer check: repeat the rule in a second context and identify what remains invariant.
A parent does not need to know the final Python collections.deque syntax. For this chapter, useful prompts are: “What did you expect from A zero-length deque stores nothing?”, “Which state changed first?”, “What evidence tests that prediction?”, and “Can the case exposing using maxlen zero as though it meant unbounded be made smaller?” The learner, not the parent, should supply the technical explanation.
Practice with an explained answer
Question. Build a tiny CCA turn order with participants cycle while absent names are removed. Include one ordinary case, one boundary and one deliberate failure caused by using maxlen zero as though it meant unbounded. Predict each result before using a tool, then report the first point where observation differs from prediction.
Answer guide. A strong response starts with the rule: deque(maxlen=0) accepts additions but immediately discards them because its capacity is zero. It shows a trace, not only a final value. The ordinary case should demonstrate “The length remains zero and iteration is empty.” The boundary must exercise the same mechanism at an edge, and the deliberate failure must be repaired with: State the contract for A zero-length deque stores nothing, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. Other data choices are valid when the evidence supports the same causal chain.
Decision and transfer
For A zero-length deque stores nothing, separate the documented Python collections.deque mechanism from the project policy. State exactly what the technical contract guarantees, then state the project choice about validation, ordering, ownership, performance or recovery. Test whether the same distinction survives one transfer case, and keep stateful experiments disposable and backed up.
Previous chapter . Contents . Next chapter
CHAPTER 10 OF 20 . Handle boundaries
10. extend and extendleft have different visible order
extend appends source items to the right in order; extendleft performs successive left appends, reversing their visible order. Treat that sentence as a testable model. A secure learner can point to the relevant input, name the operation, describe the resulting state and identify one observation that would prove the model incomplete.
The high-value mistake in this chapter is expecting extendleft([1,2,3]) to display 1,2,3 at the left. It matters because the output may look reasonable while the ownership, ordering, identity or safety rule is wrong. State the contract for extend and extendleft have different visible order, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.
For the extend and extendleft have different visible order chapter on Python collections.deque, use a two-column trace during a short Punggol home session. On the left, write the predicted state for this exact mechanism before the tool runs. On the right, record the observation that bears on expecting extendleft([1,2,3]) to display 1,2,3 at the left. Explain the earliest difference with one causal sentence, then repeat only the smallest changed case.
Core worked example
d=deque(['X']); d.extendleft([1,2,3]); print(list(d))Explained result. Successive left additions produce 3, 2, 1, X. Check the boundary as well as the happy path: ask what happens with an empty input, a duplicate or tied value, an unsupported type, a missing path, a NULL, or a second reference to the same object. Only the relevant boundary should be kept; the list is a prompt for judgment, not a demand to force every case into every example.
Four purposeful transfer cases
Case 1: revision carousel. Transfer the rule using topics rotate without rebuilding a list. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “extend appends source items to the right in order; extendleft performs successive left appends, reversing their visible order.” Apply this procedure: State the contract for extend and extendleft have different visible order, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: Successive left additions produce 3, 2, 1, X. For the revision carousel, add one near-miss that exposes expecting extendleft([1,2,3]) to display 1,2,3 at the left. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Case 2: browser history sketch. Predict the rule using visits move between left and right stacks. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “extend appends source items to the right in order; extendleft performs successive left appends, reversing their visible order.” Apply this procedure: State the contract for extend and extendleft have different visible order, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: Successive left additions produce 3, 2, 1, X. For the browser history sketch, add one near-miss that exposes expecting extendleft([1,2,3]) to display 1,2,3 at the left. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Case 3: science sensor buffer. Contrast the rule using a fixed window holds recent readings. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “extend appends source items to the right in order; extendleft performs successive left appends, reversing their visible order.” Apply this procedure: State the contract for extend and extendleft have different visible order, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: Successive left additions produce 3, 2, 1, X. For the science sensor buffer, add one near-miss that exposes expecting extendleft([1,2,3]) to display 1,2,3 at the left. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Case 4: CCA turn order. Stress-test the rule using participants cycle while absent names are removed. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “extend appends source items to the right in order; extendleft performs successive left appends, reversing their visible order.” Apply this procedure: State the contract for extend and extendleft have different visible order, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: Successive left additions produce 3, 2, 1, X. For the CCA turn order, add one near-miss that exposes expecting extendleft([1,2,3]) to display 1,2,3 at the left. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Diagnostic route
- Model check: ask the learner to draw or list the exact rows, fields, references, paths or states involved.
- Boundary check: create the smallest input that triggers expecting extendleft([1,2,3]) to display 1,2,3 at the left.
- Evidence check: separate a printed value from identity, ordering, ownership, type or repository state.
- Repair check: use the reversible procedure “State the contract for extend and extendleft have different visible order, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.” and record the first changed observation.
- Transfer check: repeat the rule in a second context and identify what remains invariant.
A parent does not need to know the final Python collections.deque syntax. For this chapter, useful prompts are: “What did you expect from extend and extendleft have different visible order?”, “Which state changed first?”, “What evidence tests that prediction?”, and “Can the case exposing expecting extendleft([1,2,3]) to display 1,2,3 at the left be made smaller?” The learner, not the parent, should supply the technical explanation.
Practice with an explained answer
Question. Build a tiny test laboratory with empty, singleton, bounded and duplicate values expose every transition. Include one ordinary case, one boundary and one deliberate failure caused by expecting extendleft([1,2,3]) to display 1,2,3 at the left. Predict each result before using a tool, then report the first point where observation differs from prediction.
Answer guide. A strong response starts with the rule: extend appends source items to the right in order; extendleft performs successive left appends, reversing their visible order. It shows a trace, not only a final value. The ordinary case should demonstrate “Successive left additions produce 3, 2, 1, X.” The boundary must exercise the same mechanism at an edge, and the deliberate failure must be repaired with: State the contract for extend and extendleft have different visible order, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. Other data choices are valid when the evidence supports the same causal chain.
Decision and transfer
For extend and extendleft have different visible order, separate the documented Python collections.deque mechanism from the project policy. State exactly what the technical contract guarantees, then state the project choice about validation, ordering, ownership, performance or recovery. Test whether the same distinction survives one transfer case, and keep stateful experiments disposable and backed up.
Previous chapter . Contents . Next chapter
rotate(n) moves rightmost items to the left for positive n and leftmost items to the right for negative n. Treat that sentence as a testable model. A secure learner can point to the relevant input, name the operation, describe the resulting state and identify one observation that would prove the model incomplete.
The high-value mistake in this chapter is reading positive rotation as movement toward larger list indices only. It matters because the output may look reasonable while the ownership, ordering, identity or safety rule is wrong. State the contract for rotate moves items cyclically, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.
For the rotate moves items cyclically chapter on Python collections.deque, use a two-column trace during a short Punggol home session. On the left, write the predicted state for this exact mechanism before the tool runs. On the right, record the observation that bears on reading positive rotation as movement toward larger list indices only. Explain the earliest difference with one causal sentence, then repeat only the smallest changed case.
Core worked example
d=deque([1,2,3,4]); d.rotate(1); print(list(d)); d.rotate(-2); print(list(d))Explained result. The first result is 4,1,2,3; the second is 2,3,4,1. Check the boundary as well as the happy path: ask what happens with an empty input, a duplicate or tied value, an unsupported type, a missing path, a NULL, or a second reference to the same object. Only the relevant boundary should be kept; the list is a prompt for judgment, not a demand to force every case into every example.
Four purposeful transfer cases
Case 1: science sensor buffer. Predict the rule using a fixed window holds recent readings. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “rotate(n) moves rightmost items to the left for positive n and leftmost items to the right for negative n.” Apply this procedure: State the contract for rotate moves items cyclically, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The first result is 4,1,2,3; the second is 2,3,4,1. For the science sensor buffer, add one near-miss that exposes reading positive rotation as movement toward larger list indices only. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Case 2: CCA turn order. Contrast the rule using participants cycle while absent names are removed. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “rotate(n) moves rightmost items to the left for positive n and leftmost items to the right for negative n.” Apply this procedure: State the contract for rotate moves items cyclically, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The first result is 4,1,2,3; the second is 2,3,4,1. For the CCA turn order, add one near-miss that exposes reading positive rotation as movement toward larger list indices only. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Case 3: test laboratory. Stress-test the rule using empty, singleton, bounded and duplicate values expose every transition. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “rotate(n) moves rightmost items to the left for positive n and leftmost items to the right for negative n.” Apply this procedure: State the contract for rotate moves items cyclically, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The first result is 4,1,2,3; the second is 2,3,4,1. For the test laboratory, add one near-miss that exposes reading positive rotation as movement toward larger list indices only. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Case 4: design decision. Explain the rule using deque is compared with list, queue.Queue and heapq. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “rotate(n) moves rightmost items to the left for positive n and leftmost items to the right for negative n.” Apply this procedure: State the contract for rotate moves items cyclically, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The first result is 4,1,2,3; the second is 2,3,4,1. For the design decision, add one near-miss that exposes reading positive rotation as movement toward larger list indices only. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Diagnostic route
- Model check: ask the learner to draw or list the exact rows, fields, references, paths or states involved.
- Boundary check: create the smallest input that triggers reading positive rotation as movement toward larger list indices only.
- Evidence check: separate a printed value from identity, ordering, ownership, type or repository state.
- Repair check: use the reversible procedure “State the contract for rotate moves items cyclically, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.” and record the first changed observation.
- Transfer check: repeat the rule in a second context and identify what remains invariant.
A parent does not need to know the final Python collections.deque syntax. For this chapter, useful prompts are: “What did you expect from rotate moves items cyclically?”, “Which state changed first?”, “What evidence tests that prediction?”, and “Can the case exposing reading positive rotation as movement toward larger list indices only be made smaller?” The learner, not the parent, should supply the technical explanation.
Practice with an explained answer
Question. Build a tiny design decision with deque is compared with list, queue.Queue and heapq. Include one ordinary case, one boundary and one deliberate failure caused by reading positive rotation as movement toward larger list indices only. Predict each result before using a tool, then report the first point where observation differs from prediction.
Answer guide. A strong response starts with the rule: rotate(n) moves rightmost items to the left for positive n and leftmost items to the right for negative n. It shows a trace, not only a final value. The ordinary case should demonstrate “The first result is 4,1,2,3; the second is 2,3,4,1.” The boundary must exercise the same mechanism at an edge, and the deliberate failure must be repaired with: State the contract for rotate moves items cyclically, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. Other data choices are valid when the evidence supports the same causal chain.
Decision and transfer
For rotate moves items cyclically, separate the documented Python collections.deque mechanism from the project policy. State exactly what the technical contract guarantees, then state the project choice about validation, ordering, ownership, performance or recovery. Test whether the same distinction survives one transfer case, and keep stateful experiments disposable and backed up.
Previous chapter . Contents . Next chapter
Rotation is cyclic, so counts larger than the length have the same effect as their remainder for a non-empty deque. Treat that sentence as a testable model. A secure learner can point to the relevant input, name the operation, describe the resulting state and identify one observation that would prove the model incomplete.
The high-value mistake in this chapter is looping thousands of manual pops without recognising the cyclic state. It matters because the output may look reasonable while the ownership, ordering, identity or safety rule is wrong. State the contract for Large rotations wrap by the deque length, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.
For the Large rotations wrap by the deque length chapter on Python collections.deque, use a two-column trace during a short Punggol home session. On the left, write the predicted state for this exact mechanism before the tool runs. On the right, record the observation that bears on looping thousands of manual pops without recognising the cyclic state. Explain the earliest difference with one causal sentence, then repeat only the smallest changed case.
Core worked example
d=deque([1,2,3]); d.rotate(7); print(list(d))Explained result. Seven right rotations equal one right rotation, producing 3,1,2. Check the boundary as well as the happy path: ask what happens with an empty input, a duplicate or tied value, an unsupported type, a missing path, a NULL, or a second reference to the same object. Only the relevant boundary should be kept; the list is a prompt for judgment, not a demand to force every case into every example.
Four purposeful transfer cases
Case 1: test laboratory. Contrast the rule using empty, singleton, bounded and duplicate values expose every transition. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “Rotation is cyclic, so counts larger than the length have the same effect as their remainder for a non-empty deque.” Apply this procedure: State the contract for Large rotations wrap by the deque length, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: Seven right rotations equal one right rotation, producing 3,1,2. For the test laboratory, add one near-miss that exposes looping thousands of manual pops without recognising the cyclic state. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Case 2: design decision. Stress-test the rule using deque is compared with list, queue.Queue and heapq. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “Rotation is cyclic, so counts larger than the length have the same effect as their remainder for a non-empty deque.” Apply this procedure: State the contract for Large rotations wrap by the deque length, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: Seven right rotations equal one right rotation, producing 3,1,2. For the design decision, add one near-miss that exposes looping thousands of manual pops without recognising the cyclic state. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Case 3: homework queue. Explain the rule using new tasks arrive at the right and the next task leaves from the left. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “Rotation is cyclic, so counts larger than the length have the same effect as their remainder for a non-empty deque.” Apply this procedure: State the contract for Large rotations wrap by the deque length, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: Seven right rotations equal one right rotation, producing 3,1,2. For the homework queue, add one near-miss that exposes looping thousands of manual pops without recognising the cyclic state. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Case 4: bus arrival window. Transfer the rule using only the latest five recorded intervals remain. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “Rotation is cyclic, so counts larger than the length have the same effect as their remainder for a non-empty deque.” Apply this procedure: State the contract for Large rotations wrap by the deque length, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: Seven right rotations equal one right rotation, producing 3,1,2. For the bus arrival window, add one near-miss that exposes looping thousands of manual pops without recognising the cyclic state. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Diagnostic route
- Model check: ask the learner to draw or list the exact rows, fields, references, paths or states involved.
- Boundary check: create the smallest input that triggers looping thousands of manual pops without recognising the cyclic state.
- Evidence check: separate a printed value from identity, ordering, ownership, type or repository state.
- Repair check: use the reversible procedure “State the contract for Large rotations wrap by the deque length, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.” and record the first changed observation.
- Transfer check: repeat the rule in a second context and identify what remains invariant.
A parent does not need to know the final Python collections.deque syntax. For this chapter, useful prompts are: “What did you expect from Large rotations wrap by the deque length?”, “Which state changed first?”, “What evidence tests that prediction?”, and “Can the case exposing looping thousands of manual pops without recognising the cyclic state be made smaller?” The learner, not the parent, should supply the technical explanation.
Practice with an explained answer
Question. Build a tiny homework queue with new tasks arrive at the right and the next task leaves from the left. Include one ordinary case, one boundary and one deliberate failure caused by looping thousands of manual pops without recognising the cyclic state. Predict each result before using a tool, then report the first point where observation differs from prediction.
Answer guide. A strong response starts with the rule: Rotation is cyclic, so counts larger than the length have the same effect as their remainder for a non-empty deque. It shows a trace, not only a final value. The ordinary case should demonstrate “Seven right rotations equal one right rotation, producing 3,1,2.” The boundary must exercise the same mechanism at an edge, and the deliberate failure must be repaired with: State the contract for Large rotations wrap by the deque length, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. Other data choices are valid when the evidence supports the same causal chain.
Decision and transfer
For Large rotations wrap by the deque length, separate the documented Python collections.deque mechanism from the project policy. State exactly what the technical contract guarantees, then state the project choice about validation, ordering, ownership, performance or recovery. Test whether the same distinction survives one transfer case, and keep stateful experiments disposable and backed up.
Previous chapter . Contents . Next chapter
Indexed access is approximately O(1) at either end but slows toward the middle to O(n). Treat that sentence as a testable model. A secure learner can point to the relevant input, name the operation, describe the resulting state and identify one observation that would prove the model incomplete.
The high-value mistake in this chapter is replacing a random-access list with deque while repeatedly reading middle positions. It matters because the output may look reasonable while the ownership, ordering, identity or safety rule is wrong. State the contract for Indexing is not uniformly fast, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.
For the Indexing is not uniformly fast chapter on Python collections.deque, use a two-column trace during a short Punggol home session. On the left, write the predicted state for this exact mechanism before the tool runs. On the right, record the observation that bears on replacing a random-access list with deque while repeatedly reading middle positions. Explain the earliest difference with one causal sentence, then repeat only the smallest changed case.
Core worked example
d=deque(range(9)); print(d[0],d[-1],d[4])Explained result. All three lookups are valid, but the middle lookup does not share the same performance promise as end access. Check the boundary as well as the happy path: ask what happens with an empty input, a duplicate or tied value, an unsupported type, a missing path, a NULL, or a second reference to the same object. Only the relevant boundary should be kept; the list is a prompt for judgment, not a demand to force every case into every example.
Four purposeful transfer cases
Case 1: homework queue. Stress-test the rule using new tasks arrive at the right and the next task leaves from the left. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “Indexed access is approximately O(1) at either end but slows toward the middle to O(n).” Apply this procedure: State the contract for Indexing is not uniformly fast, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: All three lookups are valid, but the middle lookup does not share the same performance promise as end access. For the homework queue, add one near-miss that exposes replacing a random-access list with deque while repeatedly reading middle positions. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Case 2: bus arrival window. Explain the rule using only the latest five recorded intervals remain. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “Indexed access is approximately O(1) at either end but slows toward the middle to O(n).” Apply this procedure: State the contract for Indexing is not uniformly fast, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: All three lookups are valid, but the middle lookup does not share the same performance promise as end access. For the bus arrival window, add one near-miss that exposes replacing a random-access list with deque while repeatedly reading middle positions. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Case 3: revision carousel. Transfer the rule using topics rotate without rebuilding a list. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “Indexed access is approximately O(1) at either end but slows toward the middle to O(n).” Apply this procedure: State the contract for Indexing is not uniformly fast, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: All three lookups are valid, but the middle lookup does not share the same performance promise as end access. For the revision carousel, add one near-miss that exposes replacing a random-access list with deque while repeatedly reading middle positions. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Case 4: browser history sketch. Predict the rule using visits move between left and right stacks. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “Indexed access is approximately O(1) at either end but slows toward the middle to O(n).” Apply this procedure: State the contract for Indexing is not uniformly fast, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: All three lookups are valid, but the middle lookup does not share the same performance promise as end access. For the browser history sketch, add one near-miss that exposes replacing a random-access list with deque while repeatedly reading middle positions. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Diagnostic route
- Model check: ask the learner to draw or list the exact rows, fields, references, paths or states involved.
- Boundary check: create the smallest input that triggers replacing a random-access list with deque while repeatedly reading middle positions.
- Evidence check: separate a printed value from identity, ordering, ownership, type or repository state.
- Repair check: use the reversible procedure “State the contract for Indexing is not uniformly fast, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.” and record the first changed observation.
- Transfer check: repeat the rule in a second context and identify what remains invariant.
A parent does not need to know the final Python collections.deque syntax. For this chapter, useful prompts are: “What did you expect from Indexing is not uniformly fast?”, “Which state changed first?”, “What evidence tests that prediction?”, and “Can the case exposing replacing a random-access list with deque while repeatedly reading middle positions be made smaller?” The learner, not the parent, should supply the technical explanation.
Practice with an explained answer
Question. Build a tiny bus arrival window with only the latest five recorded intervals remain. Include one ordinary case, one boundary and one deliberate failure caused by replacing a random-access list with deque while repeatedly reading middle positions. Predict each result before using a tool, then report the first point where observation differs from prediction.
Answer guide. A strong response starts with the rule: Indexed access is approximately O(1) at either end but slows toward the middle to O(n). It shows a trace, not only a final value. The ordinary case should demonstrate “All three lookups are valid, but the middle lookup does not share the same performance promise as end access.” The boundary must exercise the same mechanism at an edge, and the deliberate failure must be repaired with: State the contract for Indexing is not uniformly fast, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. Other data choices are valid when the evidence supports the same causal chain.
Decision and transfer
For Indexing is not uniformly fast, separate the documented Python collections.deque mechanism from the project policy. State exactly what the technical contract guarantees, then state the project choice about validation, ordering, ownership, performance or recovery. Test whether the same distinction survives one transfer case, and keep stateful experiments disposable and backed up.
Previous chapter . Contents . Next chapter
insert(i,x) on a bounded deque already at maxlen raises IndexError instead of evicting an opposite-end item. Treat that sentence as a testable model. A secure learner can point to the relevant input, name the operation, describe the resulting state and identify one observation that would prove the model incomplete.
The high-value mistake in this chapter is assuming every capacity-crossing method uses automatic eviction. It matters because the output may look reasonable while the ownership, ordering, identity or safety rule is wrong. State the contract for insert on a full bounded deque raises, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.
For the insert on a full bounded deque raises chapter on Python collections.deque, use a two-column trace during a short Punggol home session. On the left, write the predicted state for this exact mechanism before the tool runs. On the right, record the observation that bears on assuming every capacity-crossing method uses automatic eviction. Explain the earliest difference with one causal sentence, then repeat only the smallest changed case.
Core worked example
d=deque([1,2],maxlen=2)
try: d.insert(1,9)
except IndexError: print('full')Explained result. The deque remains unchanged because bounded insert refuses the operation. Check the boundary as well as the happy path: ask what happens with an empty input, a duplicate or tied value, an unsupported type, a missing path, a NULL, or a second reference to the same object. Only the relevant boundary should be kept; the list is a prompt for judgment, not a demand to force every case into every example.
Four purposeful transfer cases
Case 1: revision carousel. Explain the rule using topics rotate without rebuilding a list. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “insert(i,x) on a bounded deque already at maxlen raises IndexError instead of evicting an opposite-end item.” Apply this procedure: State the contract for insert on a full bounded deque raises, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The deque remains unchanged because bounded insert refuses the operation. For the revision carousel, add one near-miss that exposes assuming every capacity-crossing method uses automatic eviction. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Case 2: browser history sketch. Transfer the rule using visits move between left and right stacks. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “insert(i,x) on a bounded deque already at maxlen raises IndexError instead of evicting an opposite-end item.” Apply this procedure: State the contract for insert on a full bounded deque raises, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The deque remains unchanged because bounded insert refuses the operation. For the browser history sketch, add one near-miss that exposes assuming every capacity-crossing method uses automatic eviction. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Case 3: science sensor buffer. Predict the rule using a fixed window holds recent readings. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “insert(i,x) on a bounded deque already at maxlen raises IndexError instead of evicting an opposite-end item.” Apply this procedure: State the contract for insert on a full bounded deque raises, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The deque remains unchanged because bounded insert refuses the operation. For the science sensor buffer, add one near-miss that exposes assuming every capacity-crossing method uses automatic eviction. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Case 4: CCA turn order. Contrast the rule using participants cycle while absent names are removed. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “insert(i,x) on a bounded deque already at maxlen raises IndexError instead of evicting an opposite-end item.” Apply this procedure: State the contract for insert on a full bounded deque raises, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The deque remains unchanged because bounded insert refuses the operation. For the CCA turn order, add one near-miss that exposes assuming every capacity-crossing method uses automatic eviction. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Diagnostic route
- Model check: ask the learner to draw or list the exact rows, fields, references, paths or states involved.
- Boundary check: create the smallest input that triggers assuming every capacity-crossing method uses automatic eviction.
- Evidence check: separate a printed value from identity, ordering, ownership, type or repository state.
- Repair check: use the reversible procedure “State the contract for insert on a full bounded deque raises, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.” and record the first changed observation.
- Transfer check: repeat the rule in a second context and identify what remains invariant.
A parent does not need to know the final Python collections.deque syntax. For this chapter, useful prompts are: “What did you expect from insert on a full bounded deque raises?”, “Which state changed first?”, “What evidence tests that prediction?”, and “Can the case exposing assuming every capacity-crossing method uses automatic eviction be made smaller?” The learner, not the parent, should supply the technical explanation.
Practice with an explained answer
Question. Build a tiny revision carousel with topics rotate without rebuilding a list. Include one ordinary case, one boundary and one deliberate failure caused by assuming every capacity-crossing method uses automatic eviction. Predict each result before using a tool, then report the first point where observation differs from prediction.
Answer guide. A strong response starts with the rule: insert(i,x) on a bounded deque already at maxlen raises IndexError instead of evicting an opposite-end item. It shows a trace, not only a final value. The ordinary case should demonstrate “The deque remains unchanged because bounded insert refuses the operation.” The boundary must exercise the same mechanism at an edge, and the deliberate failure must be repaired with: State the contract for insert on a full bounded deque raises, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. Other data choices are valid when the evidence supports the same causal chain.
Decision and transfer
For insert on a full bounded deque raises, separate the documented Python collections.deque mechanism from the project policy. State exactly what the technical contract guarantees, then state the project choice about validation, ordering, ownership, performance or recovery. Test whether the same distinction survives one transfer case, and keep stateful experiments disposable and backed up.
Previous chapter . Contents . Next chapter
remove(value) deletes the first occurrence and raises ValueError when no equal item exists. Treat that sentence as a testable model. A secure learner can point to the relevant input, name the operation, describe the resulting state and identify one observation that would prove the model incomplete.
The high-value mistake in this chapter is assuming all duplicates disappear or that absence is silently ignored. It matters because the output may look reasonable while the ownership, ordering, identity or safety rule is wrong. State the contract for remove deletes the first equal value, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.
For the remove deletes the first equal value chapter on Python collections.deque, use a two-column trace during a short Punggol home session. On the left, write the predicted state for this exact mechanism before the tool runs. On the right, record the observation that bears on assuming all duplicates disappear or that absence is silently ignored. Explain the earliest difference with one causal sentence, then repeat only the smallest changed case.
Core worked example
d=deque([1,2,1]); d.remove(1); print(list(d))Explained result. Only the leftmost matching 1 is removed, leaving 2,1. Check the boundary as well as the happy path: ask what happens with an empty input, a duplicate or tied value, an unsupported type, a missing path, a NULL, or a second reference to the same object. Only the relevant boundary should be kept; the list is a prompt for judgment, not a demand to force every case into every example.
Four purposeful transfer cases
Case 1: science sensor buffer. Transfer the rule using a fixed window holds recent readings. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “remove(value) deletes the first occurrence and raises ValueError when no equal item exists.” Apply this procedure: State the contract for remove deletes the first equal value, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: Only the leftmost matching 1 is removed, leaving 2,1. For the science sensor buffer, add one near-miss that exposes assuming all duplicates disappear or that absence is silently ignored. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Case 2: CCA turn order. Predict the rule using participants cycle while absent names are removed. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “remove(value) deletes the first occurrence and raises ValueError when no equal item exists.” Apply this procedure: State the contract for remove deletes the first equal value, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: Only the leftmost matching 1 is removed, leaving 2,1. For the CCA turn order, add one near-miss that exposes assuming all duplicates disappear or that absence is silently ignored. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Case 3: test laboratory. Contrast the rule using empty, singleton, bounded and duplicate values expose every transition. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “remove(value) deletes the first occurrence and raises ValueError when no equal item exists.” Apply this procedure: State the contract for remove deletes the first equal value, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: Only the leftmost matching 1 is removed, leaving 2,1. For the test laboratory, add one near-miss that exposes assuming all duplicates disappear or that absence is silently ignored. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Case 4: design decision. Stress-test the rule using deque is compared with list, queue.Queue and heapq. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “remove(value) deletes the first occurrence and raises ValueError when no equal item exists.” Apply this procedure: State the contract for remove deletes the first equal value, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: Only the leftmost matching 1 is removed, leaving 2,1. For the design decision, add one near-miss that exposes assuming all duplicates disappear or that absence is silently ignored. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Diagnostic route
- Model check: ask the learner to draw or list the exact rows, fields, references, paths or states involved.
- Boundary check: create the smallest input that triggers assuming all duplicates disappear or that absence is silently ignored.
- Evidence check: separate a printed value from identity, ordering, ownership, type or repository state.
- Repair check: use the reversible procedure “State the contract for remove deletes the first equal value, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.” and record the first changed observation.
- Transfer check: repeat the rule in a second context and identify what remains invariant.
A parent does not need to know the final Python collections.deque syntax. For this chapter, useful prompts are: “What did you expect from remove deletes the first equal value?”, “Which state changed first?”, “What evidence tests that prediction?”, and “Can the case exposing assuming all duplicates disappear or that absence is silently ignored be made smaller?” The learner, not the parent, should supply the technical explanation.
Practice with an explained answer
Question. Build a tiny browser history sketch with visits move between left and right stacks. Include one ordinary case, one boundary and one deliberate failure caused by assuming all duplicates disappear or that absence is silently ignored. Predict each result before using a tool, then report the first point where observation differs from prediction.
Answer guide. A strong response starts with the rule: remove(value) deletes the first occurrence and raises ValueError when no equal item exists. It shows a trace, not only a final value. The ordinary case should demonstrate “Only the leftmost matching 1 is removed, leaving 2,1.” The boundary must exercise the same mechanism at an edge, and the deliberate failure must be repaired with: State the contract for remove deletes the first equal value, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. Other data choices are valid when the evidence supports the same causal chain.
Decision and transfer
For remove deletes the first equal value, separate the documented Python collections.deque mechanism from the project policy. State exactly what the technical contract guarantees, then state the project choice about validation, ordering, ownership, performance or recovery. Test whether the same distinction survives one transfer case, and keep stateful experiments disposable and backed up.
Previous chapter . Contents . Next chapter
count returns the number of equal values; index locates the first match within optional bounds and raises ValueError if absent. Treat that sentence as a testable model. A secure learner can point to the relevant input, name the operation, describe the resulting state and identify one observation that would prove the model incomplete.
The high-value mistake in this chapter is confusing a missing value with index -1 from another API. It matters because the output may look reasonable while the ownership, ordering, identity or safety rule is wrong. State the contract for count and index inspect values, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.
For the count and index inspect values chapter on Python collections.deque, use a two-column trace during a short Punggol home session. On the left, write the predicted state for this exact mechanism before the tool runs. On the right, record the observation that bears on confusing a missing value with index -1 from another API. Explain the earliest difference with one causal sentence, then repeat only the smallest changed case.
Core worked example
d=deque(['a','b','a']); print(d.count('a'),d.index('a',1))Explained result. The count is 2 and the bounded search finds the second a at index 2. Check the boundary as well as the happy path: ask what happens with an empty input, a duplicate or tied value, an unsupported type, a missing path, a NULL, or a second reference to the same object. Only the relevant boundary should be kept; the list is a prompt for judgment, not a demand to force every case into every example.
Four purposeful transfer cases
Case 1: test laboratory. Predict the rule using empty, singleton, bounded and duplicate values expose every transition. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “count returns the number of equal values; index locates the first match within optional bounds and raises ValueError if absent.” Apply this procedure: State the contract for count and index inspect values, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The count is 2 and the bounded search finds the second a at index 2. For the test laboratory, add one near-miss that exposes confusing a missing value with index -1 from another API. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Case 2: design decision. Contrast the rule using deque is compared with list, queue.Queue and heapq. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “count returns the number of equal values; index locates the first match within optional bounds and raises ValueError if absent.” Apply this procedure: State the contract for count and index inspect values, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The count is 2 and the bounded search finds the second a at index 2. For the design decision, add one near-miss that exposes confusing a missing value with index -1 from another API. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Case 3: homework queue. Stress-test the rule using new tasks arrive at the right and the next task leaves from the left. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “count returns the number of equal values; index locates the first match within optional bounds and raises ValueError if absent.” Apply this procedure: State the contract for count and index inspect values, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The count is 2 and the bounded search finds the second a at index 2. For the homework queue, add one near-miss that exposes confusing a missing value with index -1 from another API. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Case 4: bus arrival window. Explain the rule using only the latest five recorded intervals remain. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “count returns the number of equal values; index locates the first match within optional bounds and raises ValueError if absent.” Apply this procedure: State the contract for count and index inspect values, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The count is 2 and the bounded search finds the second a at index 2. For the bus arrival window, add one near-miss that exposes confusing a missing value with index -1 from another API. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Diagnostic route
- Model check: ask the learner to draw or list the exact rows, fields, references, paths or states involved.
- Boundary check: create the smallest input that triggers confusing a missing value with index -1 from another API.
- Evidence check: separate a printed value from identity, ordering, ownership, type or repository state.
- Repair check: use the reversible procedure “State the contract for count and index inspect values, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.” and record the first changed observation.
- Transfer check: repeat the rule in a second context and identify what remains invariant.
A parent does not need to know the final Python collections.deque syntax. For this chapter, useful prompts are: “What did you expect from count and index inspect values?”, “Which state changed first?”, “What evidence tests that prediction?”, and “Can the case exposing confusing a missing value with index -1 from another API be made smaller?” The learner, not the parent, should supply the technical explanation.
Practice with an explained answer
Question. Build a tiny science sensor buffer with a fixed window holds recent readings. Include one ordinary case, one boundary and one deliberate failure caused by confusing a missing value with index -1 from another API. Predict each result before using a tool, then report the first point where observation differs from prediction.
Answer guide. A strong response starts with the rule: count returns the number of equal values; index locates the first match within optional bounds and raises ValueError if absent. It shows a trace, not only a final value. The ordinary case should demonstrate “The count is 2 and the bounded search finds the second a at index 2.” The boundary must exercise the same mechanism at an edge, and the deliberate failure must be repaired with: State the contract for count and index inspect values, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. Other data choices are valid when the evidence supports the same causal chain.
Decision and transfer
For count and index inspect values, separate the documented Python collections.deque mechanism from the project policy. State exactly what the technical contract guarantees, then state the project choice about validation, ordering, ownership, performance or recovery. Test whether the same distinction survives one transfer case, and keep stateful experiments disposable and backed up.
Previous chapter . Contents . Next chapter
reverse changes the deque in place, while reversed(d) produces a reverse iterator without changing d. Treat that sentence as a testable model. A secure learner can point to the relevant input, name the operation, describe the resulting state and identify one observation that would prove the model incomplete.
The high-value mistake in this chapter is assigning the None return from reverse and losing the deque variable. It matters because the output may look reasonable while the ownership, ordering, identity or safety rule is wrong. State the contract for reverse mutates and reversed does not, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.
For the reverse mutates and reversed does not chapter on Python collections.deque, use a two-column trace during a short Punggol home session. On the left, write the predicted state for this exact mechanism before the tool runs. On the right, record the observation that bears on assigning the None return from reverse and losing the deque variable. Explain the earliest difference with one causal sentence, then repeat only the smallest changed case.
Core worked example
d=deque([1,2,3]); view=list(reversed(d)); d.reverse(); print(view,list(d))Explained result. Both displayed orders are 3,2,1, but only reverse mutates the deque. Check the boundary as well as the happy path: ask what happens with an empty input, a duplicate or tied value, an unsupported type, a missing path, a NULL, or a second reference to the same object. Only the relevant boundary should be kept; the list is a prompt for judgment, not a demand to force every case into every example.
Four purposeful transfer cases
Case 1: homework queue. Contrast the rule using new tasks arrive at the right and the next task leaves from the left. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “reverse changes the deque in place, while reversed(d) produces a reverse iterator without changing d.” Apply this procedure: State the contract for reverse mutates and reversed does not, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: Both displayed orders are 3,2,1, but only reverse mutates the deque. For the homework queue, add one near-miss that exposes assigning the None return from reverse and losing the deque variable. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Case 2: bus arrival window. Stress-test the rule using only the latest five recorded intervals remain. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “reverse changes the deque in place, while reversed(d) produces a reverse iterator without changing d.” Apply this procedure: State the contract for reverse mutates and reversed does not, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: Both displayed orders are 3,2,1, but only reverse mutates the deque. For the bus arrival window, add one near-miss that exposes assigning the None return from reverse and losing the deque variable. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Case 3: revision carousel. Explain the rule using topics rotate without rebuilding a list. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “reverse changes the deque in place, while reversed(d) produces a reverse iterator without changing d.” Apply this procedure: State the contract for reverse mutates and reversed does not, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: Both displayed orders are 3,2,1, but only reverse mutates the deque. For the revision carousel, add one near-miss that exposes assigning the None return from reverse and losing the deque variable. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Case 4: browser history sketch. Transfer the rule using visits move between left and right stacks. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “reverse changes the deque in place, while reversed(d) produces a reverse iterator without changing d.” Apply this procedure: State the contract for reverse mutates and reversed does not, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: Both displayed orders are 3,2,1, but only reverse mutates the deque. For the browser history sketch, add one near-miss that exposes assigning the None return from reverse and losing the deque variable. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Diagnostic route
- Model check: ask the learner to draw or list the exact rows, fields, references, paths or states involved.
- Boundary check: create the smallest input that triggers assigning the None return from reverse and losing the deque variable.
- Evidence check: separate a printed value from identity, ordering, ownership, type or repository state.
- Repair check: use the reversible procedure “State the contract for reverse mutates and reversed does not, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.” and record the first changed observation.
- Transfer check: repeat the rule in a second context and identify what remains invariant.
A parent does not need to know the final Python collections.deque syntax. For this chapter, useful prompts are: “What did you expect from reverse mutates and reversed does not?”, “Which state changed first?”, “What evidence tests that prediction?”, and “Can the case exposing assigning the None return from reverse and losing the deque variable be made smaller?” The learner, not the parent, should supply the technical explanation.
Practice with an explained answer
Question. Build a tiny CCA turn order with participants cycle while absent names are removed. Include one ordinary case, one boundary and one deliberate failure caused by assigning the None return from reverse and losing the deque variable. Predict each result before using a tool, then report the first point where observation differs from prediction.
Answer guide. A strong response starts with the rule: reverse changes the deque in place, while reversed(d) produces a reverse iterator without changing d. It shows a trace, not only a final value. The ordinary case should demonstrate “Both displayed orders are 3,2,1, but only reverse mutates the deque.” The boundary must exercise the same mechanism at an edge, and the deliberate failure must be repaired with: State the contract for reverse mutates and reversed does not, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. Other data choices are valid when the evidence supports the same causal chain.
Decision and transfer
For reverse mutates and reversed does not, separate the documented Python collections.deque mechanism from the project policy. State exactly what the technical contract guarantees, then state the project choice about validation, ordering, ownership, performance or recovery. Test whether the same distinction survives one transfer case, and keep stateful experiments disposable and backed up.
Previous chapter . Contents . Next chapter
copy creates a new deque with the same maxlen and references to the same nested objects. Treat that sentence as a testable model. A secure learner can point to the relevant input, name the operation, describe the resulting state and identify one observation that would prove the model incomplete.
The high-value mistake in this chapter is calling the new outer deque independent while mutating a shared child. It matters because the output may look reasonable while the ownership, ordering, identity or safety rule is wrong. State the contract for copy is shallow, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.
For the copy is shallow chapter on Python collections.deque, use a two-column trace during a short Punggol home session. On the left, write the predicted state for this exact mechanism before the tool runs. On the right, record the observation that bears on calling the new outer deque independent while mutating a shared child. Explain the earliest difference with one causal sentence, then repeat only the smallest changed case.
Core worked example
child=[]; a=deque([child],maxlen=3); b=a.copy(); child.append(1); print(list(b),b.maxlen)Explained result. b is a different deque but contains the same child list, and its maxlen remains 3. Check the boundary as well as the happy path: ask what happens with an empty input, a duplicate or tied value, an unsupported type, a missing path, a NULL, or a second reference to the same object. Only the relevant boundary should be kept; the list is a prompt for judgment, not a demand to force every case into every example.
Four purposeful transfer cases
Case 1: revision carousel. Stress-test the rule using topics rotate without rebuilding a list. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “copy creates a new deque with the same maxlen and references to the same nested objects.” Apply this procedure: State the contract for copy is shallow, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: b is a different deque but contains the same child list, and its maxlen remains 3. For the revision carousel, add one near-miss that exposes calling the new outer deque independent while mutating a shared child. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Case 2: browser history sketch. Explain the rule using visits move between left and right stacks. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “copy creates a new deque with the same maxlen and references to the same nested objects.” Apply this procedure: State the contract for copy is shallow, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: b is a different deque but contains the same child list, and its maxlen remains 3. For the browser history sketch, add one near-miss that exposes calling the new outer deque independent while mutating a shared child. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Case 3: science sensor buffer. Transfer the rule using a fixed window holds recent readings. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “copy creates a new deque with the same maxlen and references to the same nested objects.” Apply this procedure: State the contract for copy is shallow, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: b is a different deque but contains the same child list, and its maxlen remains 3. For the science sensor buffer, add one near-miss that exposes calling the new outer deque independent while mutating a shared child. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Case 4: CCA turn order. Predict the rule using participants cycle while absent names are removed. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “copy creates a new deque with the same maxlen and references to the same nested objects.” Apply this procedure: State the contract for copy is shallow, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: b is a different deque but contains the same child list, and its maxlen remains 3. For the CCA turn order, add one near-miss that exposes calling the new outer deque independent while mutating a shared child. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Diagnostic route
- Model check: ask the learner to draw or list the exact rows, fields, references, paths or states involved.
- Boundary check: create the smallest input that triggers calling the new outer deque independent while mutating a shared child.
- Evidence check: separate a printed value from identity, ordering, ownership, type or repository state.
- Repair check: use the reversible procedure “State the contract for copy is shallow, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.” and record the first changed observation.
- Transfer check: repeat the rule in a second context and identify what remains invariant.
A parent does not need to know the final Python collections.deque syntax. For this chapter, useful prompts are: “What did you expect from copy is shallow?”, “Which state changed first?”, “What evidence tests that prediction?”, and “Can the case exposing calling the new outer deque independent while mutating a shared child be made smaller?” The learner, not the parent, should supply the technical explanation.
Practice with an explained answer
Question. Build a tiny test laboratory with empty, singleton, bounded and duplicate values expose every transition. Include one ordinary case, one boundary and one deliberate failure caused by calling the new outer deque independent while mutating a shared child. Predict each result before using a tool, then report the first point where observation differs from prediction.
Answer guide. A strong response starts with the rule: copy creates a new deque with the same maxlen and references to the same nested objects. It shows a trace, not only a final value. The ordinary case should demonstrate “b is a different deque but contains the same child list, and its maxlen remains 3.” The boundary must exercise the same mechanism at an edge, and the deliberate failure must be repaired with: State the contract for copy is shallow, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. Other data choices are valid when the evidence supports the same causal chain.
Decision and transfer
For copy is shallow, separate the documented Python collections.deque mechanism from the project policy. State exactly what the technical contract guarantees, then state the project choice about validation, ordering, ownership, performance or recovery. Test whether the same distinction survives one transfer case, and keep stateful experiments disposable and backed up.
Previous chapter . Contents . Next chapter
CHAPTER 19 OF 20 . Transfer with judgment
19. Thread-safe methods are not a whole protocol
Individual append and pop operations are thread-safe in CPython documentation, but a multi-step check-then-act sequence still needs coordination. Treat that sentence as a testable model. A secure learner can point to the relevant input, name the operation, describe the resulting state and identify one observation that would prove the model incomplete.
The high-value mistake in this chapter is treating if d then popleft as one atomic transaction across threads. It matters because the output may look reasonable while the ownership, ordering, identity or safety rule is wrong. State the contract for Thread-safe methods are not a whole protocol, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.
For the Thread-safe methods are not a whole protocol chapter on Python collections.deque, use a two-column trace during a short Punggol home session. On the left, write the predicted state for this exact mechanism before the tool runs. On the right, record the observation that bears on treating if d then popleft as one atomic transaction across threads. Explain the earliest difference with one causal sentence, then repeat only the smallest changed case.
Core worked example
# A lock or queue.Queue may be needed around a compound condition and removal.
if d:
item=d.popleft()Explained result. Another thread can change state between the check and removal; method safety does not make the compound protocol atomic. Check the boundary as well as the happy path: ask what happens with an empty input, a duplicate or tied value, an unsupported type, a missing path, a NULL, or a second reference to the same object. Only the relevant boundary should be kept; the list is a prompt for judgment, not a demand to force every case into every example.
Four purposeful transfer cases
Case 1: science sensor buffer. Explain the rule using a fixed window holds recent readings. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “Individual append and pop operations are thread-safe in CPython documentation, but a multi-step check-then-act sequence still needs coordination.” Apply this procedure: State the contract for Thread-safe methods are not a whole protocol, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: Another thread can change state between the check and removal; method safety does not make the compound protocol atomic. For the science sensor buffer, add one near-miss that exposes treating if d then popleft as one atomic transaction across threads. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Case 2: CCA turn order. Transfer the rule using participants cycle while absent names are removed. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “Individual append and pop operations are thread-safe in CPython documentation, but a multi-step check-then-act sequence still needs coordination.” Apply this procedure: State the contract for Thread-safe methods are not a whole protocol, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: Another thread can change state between the check and removal; method safety does not make the compound protocol atomic. For the CCA turn order, add one near-miss that exposes treating if d then popleft as one atomic transaction across threads. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Case 3: test laboratory. Predict the rule using empty, singleton, bounded and duplicate values expose every transition. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “Individual append and pop operations are thread-safe in CPython documentation, but a multi-step check-then-act sequence still needs coordination.” Apply this procedure: State the contract for Thread-safe methods are not a whole protocol, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: Another thread can change state between the check and removal; method safety does not make the compound protocol atomic. For the test laboratory, add one near-miss that exposes treating if d then popleft as one atomic transaction across threads. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Case 4: design decision. Contrast the rule using deque is compared with list, queue.Queue and heapq. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “Individual append and pop operations are thread-safe in CPython documentation, but a multi-step check-then-act sequence still needs coordination.” Apply this procedure: State the contract for Thread-safe methods are not a whole protocol, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: Another thread can change state between the check and removal; method safety does not make the compound protocol atomic. For the design decision, add one near-miss that exposes treating if d then popleft as one atomic transaction across threads. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Diagnostic route
- Model check: ask the learner to draw or list the exact rows, fields, references, paths or states involved.
- Boundary check: create the smallest input that triggers treating if d then popleft as one atomic transaction across threads.
- Evidence check: separate a printed value from identity, ordering, ownership, type or repository state.
- Repair check: use the reversible procedure “State the contract for Thread-safe methods are not a whole protocol, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.” and record the first changed observation.
- Transfer check: repeat the rule in a second context and identify what remains invariant.
A parent does not need to know the final Python collections.deque syntax. For this chapter, useful prompts are: “What did you expect from Thread-safe methods are not a whole protocol?”, “Which state changed first?”, “What evidence tests that prediction?”, and “Can the case exposing treating if d then popleft as one atomic transaction across threads be made smaller?” The learner, not the parent, should supply the technical explanation.
Practice with an explained answer
Question. Build a tiny design decision with deque is compared with list, queue.Queue and heapq. Include one ordinary case, one boundary and one deliberate failure caused by treating if d then popleft as one atomic transaction across threads. Predict each result before using a tool, then report the first point where observation differs from prediction.
Answer guide. A strong response starts with the rule: Individual append and pop operations are thread-safe in CPython documentation, but a multi-step check-then-act sequence still needs coordination. It shows a trace, not only a final value. The ordinary case should demonstrate “Another thread can change state between the check and removal; method safety does not make the compound protocol atomic.” The boundary must exercise the same mechanism at an edge, and the deliberate failure must be repaired with: State the contract for Thread-safe methods are not a whole protocol, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. Other data choices are valid when the evidence supports the same causal chain.
Decision and transfer
For Thread-safe methods are not a whole protocol, separate the documented Python collections.deque mechanism from the project policy. State exactly what the technical contract guarantees, then state the project choice about validation, ordering, ownership, performance or recovery. Test whether the same distinction survives one transfer case, and keep stateful experiments disposable and backed up.
Previous chapter . Contents . Next chapter
CHAPTER 20 OF 20 . Transfer with judgment
20. Choose deque for an end-oriented access pattern
Deque fits queues, stacks, rotations and fixed windows; list fits frequent middle random access, queue.Queue adds blocking coordination, and heapq models priorities. Treat that sentence as a testable model. A secure learner can point to the relevant input, name the operation, describe the resulting state and identify one observation that would prove the model incomplete.
The high-value mistake in this chapter is choosing by container name instead of operations, concurrency and capacity policy. It matters because the output may look reasonable while the ownership, ordering, identity or safety rule is wrong. State the contract for Choose deque for an end-oriented access pattern, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.
For the Choose deque for an end-oriented access pattern chapter on Python collections.deque, use a two-column trace during a short Punggol home session. On the left, write the predicted state for this exact mechanism before the tool runs. On the right, record the observation that bears on choosing by container name instead of operations, concurrency and capacity policy. Explain the earliest difference with one causal sentence, then repeat only the smallest changed case.
Core worked example
recent=deque(maxlen=20)
for reading in stream: recent.append(reading)Explained result. The bounded recent-reading window makes both the access pattern and eviction policy explicit. Check the boundary as well as the happy path: ask what happens with an empty input, a duplicate or tied value, an unsupported type, a missing path, a NULL, or a second reference to the same object. Only the relevant boundary should be kept; the list is a prompt for judgment, not a demand to force every case into every example.
Four purposeful transfer cases
Case 1: test laboratory. Transfer the rule using empty, singleton, bounded and duplicate values expose every transition. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “Deque fits queues, stacks, rotations and fixed windows; list fits frequent middle random access, queue.Queue adds blocking coordination, and heapq models priorities.” Apply this procedure: State the contract for Choose deque for an end-oriented access pattern, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The bounded recent-reading window makes both the access pattern and eviction policy explicit. For the test laboratory, add one near-miss that exposes choosing by container name instead of operations, concurrency and capacity policy. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Case 2: design decision. Predict the rule using deque is compared with list, queue.Queue and heapq. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “Deque fits queues, stacks, rotations and fixed windows; list fits frequent middle random access, queue.Queue adds blocking coordination, and heapq models priorities.” Apply this procedure: State the contract for Choose deque for an end-oriented access pattern, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The bounded recent-reading window makes both the access pattern and eviction policy explicit. For the design decision, add one near-miss that exposes choosing by container name instead of operations, concurrency and capacity policy. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Case 3: homework queue. Contrast the rule using new tasks arrive at the right and the next task leaves from the left. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “Deque fits queues, stacks, rotations and fixed windows; list fits frequent middle random access, queue.Queue adds blocking coordination, and heapq models priorities.” Apply this procedure: State the contract for Choose deque for an end-oriented access pattern, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The bounded recent-reading window makes both the access pattern and eviction policy explicit. For the homework queue, add one near-miss that exposes choosing by container name instead of operations, concurrency and capacity policy. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Case 4: bus arrival window. Stress-test the rule using only the latest five recorded intervals remain. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.
Reasoned route. Begin with “Deque fits queues, stacks, rotations and fixed windows; list fits frequent middle random access, queue.Queue adds blocking coordination, and heapq models priorities.” Apply this procedure: State the contract for Choose deque for an end-oriented access pattern, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The bounded recent-reading window makes both the access pattern and eviction policy explicit. For the bus arrival window, add one near-miss that exposes choosing by container name instead of operations, concurrency and capacity policy. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.
Diagnostic route
- Model check: ask the learner to draw or list the exact rows, fields, references, paths or states involved.
- Boundary check: create the smallest input that triggers choosing by container name instead of operations, concurrency and capacity policy.
- Evidence check: separate a printed value from identity, ordering, ownership, type or repository state.
- Repair check: use the reversible procedure “State the contract for Choose deque for an end-oriented access pattern, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.” and record the first changed observation.
- Transfer check: repeat the rule in a second context and identify what remains invariant.
A parent does not need to know the final Python collections.deque syntax. For this chapter, useful prompts are: “What did you expect from Choose deque for an end-oriented access pattern?”, “Which state changed first?”, “What evidence tests that prediction?”, and “Can the case exposing choosing by container name instead of operations, concurrency and capacity policy be made smaller?” The learner, not the parent, should supply the technical explanation.
Practice with an explained answer
Question. Build a tiny homework queue with new tasks arrive at the right and the next task leaves from the left. Include one ordinary case, one boundary and one deliberate failure caused by choosing by container name instead of operations, concurrency and capacity policy. Predict each result before using a tool, then report the first point where observation differs from prediction.
Answer guide. A strong response starts with the rule: Deque fits queues, stacks, rotations and fixed windows; list fits frequent middle random access, queue.Queue adds blocking coordination, and heapq models priorities. It shows a trace, not only a final value. The ordinary case should demonstrate “The bounded recent-reading window makes both the access pattern and eviction policy explicit.” The boundary must exercise the same mechanism at an edge, and the deliberate failure must be repaired with: State the contract for Choose deque for an end-oriented access pattern, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. Other data choices are valid when the evidence supports the same causal chain.
Decision and transfer
For Choose deque for an end-oriented access pattern, separate the documented Python collections.deque mechanism from the project policy. State exactly what the technical contract guarantees, then state the project choice about validation, ordering, ownership, performance or recovery. Test whether the same distinction survives one transfer case, and keep stateful experiments disposable and backed up.
Previous chapter . Contents . Next chapter
Parent guide: choose the next useful step
Start with evidence, not a label such as careless. Ask for one prediction and one trace. If the first transition is wrong, rebuild the model. If the model is sound but syntax fails, practise reference use. If routine cases are correct but boundaries fail, vary ties, defaults, unsupported inputs, ownership or missing paths. If explanations transfer, move to a small project.
Keep a weekly record with four lines: concept, prediction, observed difference and next test. Stop when fatigue replaces reasoning. A smaller case tomorrow is more useful than another hour of copying tonight.
Seek specialist help when cause and effect remain invisible after examples are reduced, when accessibility or data-loss implications are unclear, or when an important repository, database or application state may be at risk. Good support should make the learner’s reasoning more independent.
Capstone practice with explained routes
1. homework queue: model, boundary and recovery
Create a small homework queue using new tasks arrive at the right and the next task leaves from the left. Combine “Construction preserves iterable order” with one later chapter. Include an ordinary case, a boundary, a deliberate failure and a recovery. Write the expected state before each operation.
Explained route. Start with: deque(iterable) appends source items from left to right, so iteration initially matches the iterable. Apply: State the contract for Construction preserves iterable order, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. Verify: The visible order is A, B, C; A is leftmost and C is rightmost. Then add a second chapter whose boundary could change the outcome. A complete solution contains the input model, a trace, observed evidence, a correction and one transfer statement. The exact data may differ; the causal chain must be checkable.
2. bus arrival window: model, boundary and recovery
Create a small bus arrival window using only the latest five recorded intervals remain. Combine “pop and popleft remove different ends” with one later chapter. Include an ordinary case, a boundary, a deliberate failure and a recovery. Write the expected state before each operation.
Explained route. Start with: pop removes and returns the rightmost item, while popleft removes and returns the leftmost item. Apply: State the contract for pop and popleft remove different ends, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. Verify: The calls return A then C, leaving B. Then add a second chapter whose boundary could change the outcome. A complete solution contains the input model, a trace, observed evidence, a correction and one transfer statement. The exact data may differ; the causal chain must be checkable.
3. revision carousel: model, boundary and recovery
Create a small revision carousel using topics rotate without rebuilding a list. Combine “maxlen creates a bounded deque” with one later chapter. Include an ordinary case, a boundary, a deliberate failure and a recovery. Write the expected state before each operation.
Explained route. Start with: A non-None maxlen fixes the capacity and makes new additions discard items from the opposite end when full. Apply: State the contract for maxlen creates a bounded deque, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. Verify: Appending at the right discards 1 from the left, leaving 2, 3, 4. Then add a second chapter whose boundary could change the outcome. A complete solution contains the input model, a trace, observed evidence, a correction and one transfer statement. The exact data may differ; the causal chain must be checkable.
4. browser history sketch: model, boundary and recovery
Create a small browser history sketch using visits move between left and right stacks. Combine “extend and extendleft have different visible order” with one later chapter. Include an ordinary case, a boundary, a deliberate failure and a recovery. Write the expected state before each operation.
Explained route. Start with: extend appends source items to the right in order; extendleft performs successive left appends, reversing their visible order. Apply: State the contract for extend and extendleft have different visible order, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. Verify: Successive left additions produce 3, 2, 1, X. Then add a second chapter whose boundary could change the outcome. A complete solution contains the input model, a trace, observed evidence, a correction and one transfer statement. The exact data may differ; the causal chain must be checkable.
5. science sensor buffer: model, boundary and recovery
Create a small science sensor buffer using a fixed window holds recent readings. Combine “Indexing is not uniformly fast” with one later chapter. Include an ordinary case, a boundary, a deliberate failure and a recovery. Write the expected state before each operation.
Explained route. Start with: Indexed access is approximately O(1) at either end but slows toward the middle to O(n). Apply: State the contract for Indexing is not uniformly fast, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. Verify: All three lookups are valid, but the middle lookup does not share the same performance promise as end access. Then add a second chapter whose boundary could change the outcome. A complete solution contains the input model, a trace, observed evidence, a correction and one transfer statement. The exact data may differ; the causal chain must be checkable.
6. CCA turn order: model, boundary and recovery
Create a small CCA turn order using participants cycle while absent names are removed. Combine “count and index inspect values” with one later chapter. Include an ordinary case, a boundary, a deliberate failure and a recovery. Write the expected state before each operation.
Explained route. Start with: count returns the number of equal values; index locates the first match within optional bounds and raises ValueError if absent. Apply: State the contract for count and index inspect values, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. Verify: The count is 2 and the bounded search finds the second a at index 2. Then add a second chapter whose boundary could change the outcome. A complete solution contains the input model, a trace, observed evidence, a correction and one transfer statement. The exact data may differ; the causal chain must be checkable.
7. test laboratory: model, boundary and recovery
Create a small test laboratory using empty, singleton, bounded and duplicate values expose every transition. Combine “Thread-safe methods are not a whole protocol” with one later chapter. Include an ordinary case, a boundary, a deliberate failure and a recovery. Write the expected state before each operation.
Explained route. Start with: Individual append and pop operations are thread-safe in CPython documentation, but a multi-step check-then-act sequence still needs coordination. Apply: State the contract for Thread-safe methods are not a whole protocol, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. Verify: Another thread can change state between the check and removal; method safety does not make the compound protocol atomic. Then add a second chapter whose boundary could change the outcome. A complete solution contains the input model, a trace, observed evidence, a correction and one transfer statement. The exact data may differ; the causal chain must be checkable.
8. design decision: model, boundary and recovery
Create a small design decision using deque is compared with list, queue.Queue and heapq. Combine “append adds at the right” with one later chapter. Include an ordinary case, a boundary, a deliberate failure and a recovery. Write the expected state before each operation.
Explained route. Start with: append(x) adds exactly one item to the right end. Apply: State the contract for append adds at the right, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. Verify: The deque becomes A, B, C and the previous left end is unchanged. Then add a second chapter whose boundary could change the outcome. A complete solution contains the input model, a trace, observed evidence, a correction and one transfer statement. The exact data may differ; the causal chain must be checkable.
Frequently asked questions
How long should a practice session be?
Use one complete prediction–observation–explanation cycle while attention remains good. Ten to twenty focused minutes can be enough.
Should every option or function be memorised?
No. Memorise the governing distinctions and practise retrieving the official reference. Understanding means predicting and explaining, not reciting a parameter list.
What if the result is correct but the explanation is weak?
Treat it as partial success. Ask for a trace and change one boundary. A reliable model survives controlled variation.
Is the shortest solution the best?
Not automatically. Prefer the solution whose semantics, failure modes and maintenance cost are easiest to justify for the actual project.
When should official documentation be used?
Use it whenever syntax, supported types, SQL dialect behaviour or Git version details matter. Primary documentation settles the current contract.
How can a parent help without technical expertise?
Ask what was predicted, where the first difference appeared, what evidence matters and which smaller example could isolate it.
How do we test transfer?
Change the context, vocabulary and one boundary. Require the learner to identify the invariant before using a tool.
What should be saved after practice?
Keep the corrected rule, one trace, one boundary case and the next question. Avoid storing pages of unexplained output.
Can these exercises replace backups?
No. Use disposable examples and proper backups. Learning should not endanger schoolwork, repositories or personal data.
What counts as mastery?
The learner can predict, verify, diagnose, recover and justify a choice across more than one context, while knowing when to consult the current reference.

