Question
write a function copy(grid) that creates and returns a deep copy of grida new, separate 2-D list that has the same dimensions and cell values
write a function copy(grid) that creates and returns a deep copyof grida new, separate 2-D list that has the same dimensions and cell values as grid. Note that you cannot just perform a full slice on grid (e.g., grid[:]), because you would still end up with copies of the references to the rows! Instead, you should do the following:
Use create_grid to create a new 2-D list with the same dimensions as grid, and assign it to an appropriately named variable. (Dont call your new list grid, since that is the name of the parameter!) Remember that len(grid) will give you the number of rows in grid, and len(grid[0]) will give you the number of columns.
Use nested loops to copy the individual values from the cells of grid into the cells of your newly created grid.
Make sure to return the newly created grid and not the original one!
To test that your copy function is working properly, try the following examples:
>>> grid1 = diagonal_grid(3, 3) >>> print_grid(grid1) 100 010 001 >>> grid2 = copy(grid1) # should get a deep copy of grid1 >>> print_grid(grid2) 100 010 001 >>> grid1[0][1] = 1 >>> print_grid(grid1) # should see an extra 1 at [0][1] 110 010 001 >>> print_grid(grid2) # should not see an extra 1 100 010 001
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started