—
Trace aliasing, shallow copy, and equality
Tutor me on https://mlprep.iwase.dev/item/prog-py-alias-copy-equality/. This is a screening item. If window.mlPrepAgent is available, read attempts for item prog-py-alias-copy-equality before tutoring. Inspect my attempt, keep the item ID, and do not reveal the full answer first. After a real attempt, append its record with recorded_by agent, include agent_session_id when available, and read it back.Problem
Predict every component returned by alias_copy_report(). Explain which
mutations reach b and c, and distinguish == from is.
Reveal answer or reference solution
b aliases the outer list, so it sees both mutations. c has a distinct
outer list but shares the original inner lists, so it sees the 3 added
inside the first inner list but not the new [4] appended to the outer
list. a == b and a is b are true. a == c is false because their
outer contents differ. c[0] is a[0] is true.
def alias_copy_report() -> tuple[object, ...]:
a = [[1], [2]]
b = a
c = a.copy()
a[0].append(3)
a.append([4])
return a, b, c, a == b, a is b, a == c, c[0] is a[0]Public tests
alias_copy_report()→([[1, 3], [2], [4]], [[1, 3], [2], [4]], [[1, 3], [2]], True, True, False, True)
Previous attempts
Loading attempts saved in this browser…