Bug hunt

The Shared Backpack

b = a does not copy a list. It puts a second name tag on the same list. Change it through one name, and the other "changes" too. This is called aliasing.

team = ["Pip", "Ada"]
trial = team          # same list, two names
print(trial is team)  # True

To get a real copy, use team.copy() (or list(team), or team[:]).

But there's a trap: .copy() is shallow. It copies the outer list, but lists inside it are still shared:

import copy
grid = [[".", "."], [".", "."]]
shallow = grid.copy()
shallow[0][0] = "S"       # grid[0] changes too!
deep = copy.deepcopy(grid)  # copies every level

Detective tool: x is y tells you if two names point at the same object.

The bug report

  • start_trial(team) should return a new team with "ghost" added. But the real team gains a ghost too!
  • plan_route(grid) should return a copy of the map with "S" at the top-left. But the original map gets an "S" as well.

Assignment

Fix both functions so the list passed in is never changed. Every row of the plan must be a separate list from the original's rows.