Question
Python-2D lists! Please do not import any fancy functions..this is only an intro class and we haven't gotten that far yet Write a function invert(grid)
Python-2D lists!
Please do not import any fancy functions..this is only an intro class and we haven't gotten that far yet
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 not create 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 not have a return statement, because it doesnt need one! Thats because its parameter grid 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)
>>> print_grid(grid1)
00000
01110
01110
01110
00000
>>> grid2 = grid1
>>> grid3 = grid1[:]
>>> invert(grid1)
>>> print_grid(grid1)
11111
10001
10001
10001
11111
This is what I have from previous problems:
import random
def create_grid(height, width): """ creates and returns a 2-D list of 0s with the specified dimensions. inputs: height and width are non-negative integers """ grid = [] for r in range(height): row = [0] * width # a row containing width 0s grid += [row]
return grid
def print_grid(grid): """ prints the 2-D list specified by grid in 2-D form, with each row on its own line, and nothing between values. input: grid is a 2-D list. We assume that all of the cell values are integers between 0 and 9. """ height = len(grid) width = len(grid[0]) for r in range(height): for c in range(width): print(grid[r][c], end='') # print nothing between values print() # at end of row, go to next line
def diagonal_grid(height, width): """ creates and returns a height x width grid in which the cells on the diagonal are set to 1, and all other cells are 0. inputs: height and width are non-negative integers """ grid = create_grid(height, width) # initially all 0s
for r in range(height): for c in range(width): if r == c: grid[r][c] = 1
return grid #part 1 def inner_grid(height,width): """creates and returns a 2-D list of height rows and width columns in which the "" """ grid=create_grid(height,width)
for r in range(1,height-1): for c in range(1,width-1): if r>0 and r return grid def print_grid(grid): """ prints the 2-D list specified by grid in 2-D form, with each row on its own line, and nothing between values. input: grid is a 2-D list. We assume that all of the cell values are integers between 0 and 9. """ height = len(grid) width = len(grid[0]) for r in range(height): for c in range(width): print(grid[r][c], end='') # print nothing between values print() # at end of row, go to next line def create_new_grid(height,width): """creates a new grid """ new_grid=[] for r in range(height): row = [0] * width # a row containing width 0s new_grid += [row] return new_grid def print_new_grid(grid): """ prints the 2-D list specified by grid in 2-D form, with each row on its own line, and nothing between values. input: grid is a 2-D list. We assume that all of the cell values are integers between 0 and 9. """ height = len(new_grid) width = len(new_grid[0]) for r in range(height): for c in range(width): print(new_grid[r][c], end='') # print nothing between values print() # at end of row, go to next line def copy(grid): """creates and returns a deep copy of grid, a new, separate 2-D list that has the same dimensions and cell values as grid """ new_grid=[] height = len(grid) width = len(grid[0]) new_grid=create_new_grid(height,width) for r in range(height): for c in range(width): new_grid[r][c]=grid[r][c] return new_grid
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