# # ps6pr5.py (Problem Set 6, Problem 5) # # 2-D Lists # # Computer Science 111 # import random def create_grid(num_rows, num_cols): """ creates and returns a 2-D list of 0s with the specified dimensions. inputs: num_rows and num_cols are non-negative integers """ grid = [] for r in range(num_rows): row = [0] * num_cols # 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. input: grid is a 2-D list """ num_rows = len(grid) for r in range(num_rows): if r == 0: print('[', end='') else: print(' ', end='') if r < num_rows - 1: print(str(grid[r]) + ',') else: print(str(grid[r]) + ']') def random_grid(num_rows, num_cols): """ creates and returns a 2-D list with the specified dimensions in which each cell is assigned a random integer from 0-9. inputs: num_rows and num_cols are non-negative integers """ grid = create_grid(num_rows, num_cols) for r in range(num_rows): for c in range(num_cols): grid[r][c] = random.choice(range(10)) return grid