Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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
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
3. Write a function increment(grid) that takes an existing 2-D list of digits and increments each digit by 1. If incrementing a given digit causes it to become a 10 (i.e., if the original digit was a 9), then the new digit should "wrap around" and become a 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 doesn't need one! That's 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 wil 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 cells. For example 5) >>> grid = diagonal-grid(5, >>>print_grid(grid) 10000 01000 00100 00010 00001 >increment(grid) >>>print_grid(grid) 12111 11211 11121 >>>increment(grid) >>>print_grid(grid) 32222 23222 22322 22232
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