Rows, columns, diagonals
A grid is a list of lists. grid[r] is row r, and grid[r][c] is the cell in row r, column c. Row 0 is the top, column 0 is the left.
grid = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] grid[1] # row 1: [4, 5, 6] [row[2] for row in grid] # column 2: [3, 6, 9] grid[1][1] # a cell: 5
A row is already a list. A column is the same position in every row, so you loop over the rows. On the main diagonal the row and column are equal: grid[i][i]. On the anti-diagonal they add up to n - 1: grid[i][n - 1 - i]. Cells with the same c - r share a down-right diagonal.
Always get rows with len(grid) and columns with len(grid[0]). They are not the same number unless the grid is square.
Watch the visual: the row is one list lookup, the column needs a loop.
See it run
grid = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] row = grid[1] col = [] for r in range(3): col.append(grid[r][2]) diag = [grid[0][0], grid[1][1], grid[2][2]] print(row, col, diag)
A grid is a list whose items are rows.
Practice rows, columns, diagonals
Exercise 1
A seed tray is a grid: each inner list is one row, and each number is how many seeds were planted in that cell.
Return a list with the total seeds in each column, from the left column to the right column.
Rules:
- Every row has the same length. There is at least 1 row and 1 column.
- Do not change
tray.
Constraints: 1 ≤ rows, columns ≤ 100; 0 ≤ seeds ≤ 1000.
Example: solution([[1, 2, 3], [4, 5, 6]]) returns [5, 7, 9] because the columns hold 1+4, 2+5 and 3+6.
Exercise 2
A lamp panel is an n×n grid of 0 (off) and 1 (on). A line is lit when every lamp in it is on. The lines are: each of the n rows, each of the n columns, the main diagonal (top-left to bottom-right) and the anti-diagonal (top-right to bottom-left).
Return how many lines are lit.
Rules:
- The two diagonals are always counted as two separate lines, even when n = 1. A 1×1 panel with its lamp on has 4 lit lines: its row, its column and both diagonals.
- Do not change the panel.
Constraints: 1 ≤ n ≤ 50.
Example: solution([[1, 0, 1], [0, 1, 0], [1, 0, 1]]) returns 2: no row or column is fully on, but both diagonals are.
Exercise 3
A roof is a rectangular grid of solar panels: roof[r][c] is the output in watts of the panel in row r (row 0 is the top) and column c (column 0 is the left). Late sun shades whole down-right diagonals: the cells (r, c), (r+1, c+1), (r+2, c+2), ... share one diagonal.
Return a list with the total watts of every down-right diagonal, ordered from the diagonal that is only the bottom-left cell to the diagonal that is only the top-right cell.
Rules:
- A grid with R rows and C columns has R + C − 1 diagonals, so the list has that length.
- Cells
(r, c)with the same value ofc − rare on the same diagonal, and the order is by increasingc − r. - Do not change
roof.
Constraints: 1 ≤ R, C ≤ 200; 0 ≤ watts ≤ 1000.
Example: solution([[1, 2, 3], [4, 5, 6]]) returns [4, 6, 8, 3]: bottom-left 4; then 1+5; then 2+6; then top-right 3.
The explanation and tasks are public. Running this lesson requires an account with course access.
Practice
Loading Python…Visualizer Watch your code run line by line
Run your code, then choose Play to follow its execution.