Practice Solutions and Feedback
Open after an independent attempt. Alternative structures earn full credit when their contracts and evidence match.
P01
A dollars result such as 9 for a 1000-cent input may satisfy a numeric range but violates the unit contract. Signature and simple bounds cannot catch every bad policy. L01 deliberately checks boundary behavior; policy-specific arithmetic needs its own oracle.
P02
Live capture reads the changed dictionary value. Capture the numeric rate into an immutable local for a fixed policy; copying an outer mapping is insufficient if its nested value remains mutable. Neither choice is inherently better without a lifetime contract.
P03
A narrow callable seam can isolate slow/nondeterministic I/O with one provider. Keep provider creation at composition time. A pure fixed formula already testable as a function may need no new interface. Implementation count is not the criterion.
P04
Load and normalize run; render raises; persist is skipped; guaranteed cleanup runs in finally. If load fails before acquiring anything, cleanup must respect that. @final informs Python type checking and does not prohibit runtime overriding.
P05
A cycle makes no globally consistent ordering possible. Freeze time/configuration at the required scope and explicitly define ties/invalid dates. A callable and class can each satisfy or violate the same laws; method shape is insufficient.
P06
One registration remains and receives one call. A token per subscribe call permits idempotent disposal without removing the other registration. Searching by callback equality and removing on each call breaks this.
P07
Snapshot trace: A1,B1,A2,C2. Immediate suppression: A1,A2,C2 if additions also wait until next publication. State both removal and addition policies: immediate addition could produce yet another trace.
P08
A recursive publish fails before inner delivery; its RuntimeError is collected, then B's ValueError, then C runs. Removing A permits later events. A try/finally reset prevents one exceptional publication from permanently blocking the subject.
P09
An immutable pushed version-4 event retains that observation. A later pull may see version 5. A shared mutable pushed object can also change, so push alone is not a snapshot guarantee. Choose a version/payload contract deliberately.
P10
Each disposed registration must stop receiving subsequent events; the persistent one remains. Associate cancellation with an owner/generation. Coalescing latest-view updates may be acceptable; dropping accepted ledger entries generally violates the record contract. No framework choice supplies this automatically.
P11
Text moves a -> ab -> a -> a -> ab. Invalid edit leaves redo available. A model versions=[a,ab], cursor=0 after undo makes the expected redo clear. Validate before mutating either history or text.
P12
Restoring old text with a cursor past the new end violates the document invariant. Capture all public state or derive it under a documented rule. Deep copies, deltas and shared immutable versions have different storage and identity costs; count retained reachable state.
P13
Attempts are archive,submit,approve. Accepted facts are draft/submit/review and review/approve/published. Mark-before-send can lose work, mark-after-send can duplicate it after acknowledgement loss. Use an effect-specific idempotency/reconciliation contract; local replay invokes no send.
P14
Four allowed cells are draft+submit, review+revise, review+approve, published+archive; the other twelve reject unchanged. Unknown event/state use distinct validation errors. A generation number distinguishes review instances that share the same state label.
P15
Exact errors and status access are separate observations. Preserve a derived public label and any documented setter behavior while keeping one authority. Choose representation based on behavior density and change pressure, not a claim that no switches is inherently better.
P16
No calls on construction; first next fetches 0 and yields 10; second yields 20 without another fetch. Third next fetches 1, sees empty+more, fetches 2 and yields 30. Stopping after second next means only page 0 was fetched.
P17
In first-match policy an early allow stops and ignores later denial. In mandatory-check validation, success continues and only final success allows. All-pass in L06 denies; error and malformed decisions stop without executing later handlers. Do not silently mix these protocols.
P18
Value 20; fully parenthesized output ((2 + 3) * 4). Count adds one visitor with all existing node methods. Neg adds a node and support to every visitor. An exhaustive switch has the same operation/type tradeoff with different syntax.
P19
For local+pickup, no address is required and submit is true for either address. Local+post requires a nonempty address. Abroad normalizes either requested method to post and requires a nonempty address. Rendering must not republish equivalent user-change events; stale responses need a generation/lifetime check.
P20
AND evaluates right only when left is true; OR evaluates right only when left is false; NOT evaluates one operand and negates it. Unknown names fail only when evaluated under this lesson's contract. Reject malformed AST shape explicitly before treating a tag as an operation; textual parsing is separate.