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
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)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.