cs-044 — The Aliasing Trap

FieldValue
idcs-044
difficultyAdvanced
topicslists, aliasing, debugging
lecturelecture-11
estimated time5-minute attempt + 10-minute discussion
prerequisitesfunctions, dictionaries, and earlier algorithmic patterns
objectivedesign the algorithm, verify it on edges, and argue correctness or trade-offs
solutionheld by the instructor; discussed after the attempt

Problem

A matrix is built as [[0]3]2 and rows mutate together. Explain why, then build it correctly. Aliasing as the deepest beginner trap.

Your deliverable is the diagnosis of why the two rows mutate together, the corrected construction, and a written prediction of the same trap for a matrix built with append in a loop. If the problem is ambiguous anywhere, state the assumption you are making and carry on — a stated assumption is part of a good answer.

Examples

Solve for the worked instance first. Then ask what would change if one number, name, or condition were replaced by something stranger.

What to notice

Discussion starter

After the solution is revealed, the class spends five minutes on this prompt: "When is the extra memory of slicing worth it, and when would you insist on in-place?"

Constraints and assumptions

Hints and extension