Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

def diagonal_grid(height, width): \"\"\" creates and returns a height x width gr

ID: 3738092 • Letter: D

Question

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

Explanation / Answer

def increment(grid): for r in range(len(grid)): for c in range(len(grid[r])): grid[r][c] += 1 if grid[r][c] >= 10: grid[r][c] = 10 - grid[r][c] def create_grid(height, width): list1 = [[0 for c in range(width)] for r in range(height)] return list1 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 def print_grid(grid): for r in range(len(grid)): for c in range(len(grid[r])): print(grid[r][c], end="") print() print() grid = diagonal_grid(5,5) print_grid(grid) increment(grid) print_grid(grid) increment(grid)