Question: 1. Based on the example of diagonal_grid, write a function inner_grid(height, width, digit) that creates and returns a 2-D list of height rows and width

1. Based on the example of diagonal_grid, write a function inner_grid(height, width, digit) that creates and returns a 2-D list of height rows and width columns in which the inner cells all have a value of digitwhich you may assume is an integer between 0and 9 inclusiveand the cells on the outer border are all 0.

For example:

>>> grid = inner_grid(5, 5, 7) >>> print_grid(grid) 00000 07770 07770 07770 00000 >>> grid = inner_grid(6, 4, 3) >>> print_grid(grid) 0000 0330 0330 0330 0330 0000 

Hint: Modify the ranges used by your loops so that they loop over only the inner cells.

2. As weve seen in lecture, copying a list variable does not actually copy the list. To see an example of this, try the following commands from the Shell:

>>> grid1 = create_grid(2, 2) >>> grid2 = grid1 # copy grid1 into grid2 >>> print_grid(grid2) 00 00 >>> grid1[0][0] = 1 >>> print_grid(grid1) 10 00 >>> print_grid(grid2) 10 00 

Note that changing grid1 also changes grid2! Thats because the assignment grid2 = grid1 did not copy the list represented by grid1; it copied the reference to the list. Thus, grid1 and grid2 both refer to the same list!

To avoid this problem, 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 

Write a function invert(grid) that takes an existing 2-D list of 0s and 1s and inverts it changing all 0 values to 1, and changing all 1 values to 0.

Important notes:

Unlike the other functions that you wrote for this problem, this function should notcreate and return a new 2-D list. Rather, it should modify the internals of the existing list.

Unlike the other functions that you wrote for this problem, this function should nothave a return statement, because it doesnt need one! Thats because its parametergrid gets a copy of the reference to the original 2-D list, and thus any changes that it makes to the internals of that list will still be visible after the function returns.

The loops in this function need to loop over all of the cells in the grid, not just the inner ones.

For example:

>>> grid = diagonal_grid(5, 5) >>> print_grid(grid) 10000 01000 00100 00010 00001 >>> invert(grid) >>> print_grid(grid) 01111 10111 11011 11101 11110 

Heres another example that should help to reinforce your understanding of references:

>>> grid1 = inner_grid(5, 5, 1) >>> print_grid(grid1) 00000 01110 01110 01110 00000 >>> grid2 = grid1 >>> grid3 = grid1[:] >>> invert(grid1) >>> print_grid(grid1) 11111 10001 10001 10001 11111 

As you can see above, the value of grid1 has been changed. What about grid2 and grid3?

Before entering the statements below, see if you can predict what has happened to grid2and grid3. If we print them, will we see the original grid or an inverted one?

Test your understanding by first entering the following:

>>> print_grid(grid2) 

What do you see? Why does this make sense?

Now enter the following:

>>> print_grid(grid3) 

What do you see? Why does this make sense?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!