1
Hatchling0 / 300 XP
0day streak 0stars
Your Python practice
Mock tests Readiness
More
Guest mode · saved on this device.

Transpose and rotate

To transpose a grid, swap rows and columns: the result's cell [c][r] is the original's [r][c]. An R-row, C-column grid becomes C rows by R columns.

To rotate a quarter turn clockwise, the old left column, read bottom to top, becomes the new top row: new[i][j] = old[R - 1 - j][i]. Four turns return the original, so use k % 4.

Both are easiest as new grids: build every row as a fresh list and leave the input alone. An in place rotation is only possible when the grid is square (R = C), because a non-square grid changes shape.

Two traps: [[0] * 3] * 2 makes two names for the same row, so changing one changes both. And rows are lists, so new = old or old[:] still shares the rows.

Watch the visual: the mirror has its own rows, so changing it leaves the original alone.

See it run

grid = [[1, 2, 3], [4, 5, 6]]
mirror = []
for row in grid:
    mirror.append(row[::-1])
mirror[0][0] = 9
print(grid[0][0], mirror[0][0])

We build a mirrored copy, one reversed row at a time.

Practice transpose and rotate

Exercise 1

A seating chart lists the seats row by row: chart[r][c] is the name in row r (front row first), seat c (left seat first). The ushers want it listed by aisle instead: seat 0 of every row first, then seat 1 of every row, and so on.

Return a new grid out where out[c][r] is chart[r][c]. (This is the transpose: rows become columns.)

Rules:

  • The chart is rectangular, with at least 1 row and 1 seat. With R rows and C seats, the result has C rows and R seats.
  • Build and return a new grid; chart is left unchanged.

Constraints: 1 ≤ R, C ≤ 100.

Example: solution([["Ann", "Bo"], ["Cy", "Di"], ["Ed", "Flo"]]) returns [["Ann", "Cy", "Ed"], ["Bo", "Di", "Flo"]].

Exercise 2

A ticker board is a grid of symbols. Row r (row 0 is the top) is rotated to the right by r × step cells: symbols pushed off the right end re-enter at the left, in the same order.

Return the rotated board as a new grid.

Rules:

  • step may be 0, negative (a negative amount rotates left) or larger than the row length; rotating by the row length changes nothing.
  • Row 0 is never moved. The board is rectangular with at least 1 row and 1 column.
  • Do not change board.

Constraints: 1 ≤ rows, columns ≤ 100; -1000 ≤ step ≤ 1000.

Example: solution([[1, 2, 3], [4, 5, 6], [7, 8, 9]], 1) returns [[1, 2, 3], [6, 4, 5], [8, 9, 7]]: row 1 moves right by 1, row 2 by 2.

Exercise 3

A stage map is a rectangular grid. Return a new grid showing the map after it is turned k quarter-turns clockwise.

Rules:

  • One clockwise quarter-turn of a grid with R rows and C columns gives a grid with C rows and R columns, where new[i][j] = old[R − 1 − j][i]. The old left column, read bottom to top, becomes the new top row.
  • k may be 0 (return a copy of the map). Four turns bring the map back to the start.
  • Do not change the input grid.

Constraints: 1 ≤ R, C ≤ 100; 0 ≤ k ≤ 10^9. The performance test turns a small map many, many times.

Example: solution([[1, 2, 3], [4, 5, 6]], 1) returns [[4, 1], [5, 2], [6, 3]]. With k = 2 it returns [[6, 5, 4], [3, 2, 1]].

The explanation and tasks are public. Running this lesson requires an account with course access.

Practice

Loading Python…
More
Loading exercise content…
Visualizer Watch your code run line by line

Run your code, then choose Play to follow its execution.

Memory · names → values
Variables appear here as Python creates them.
Output & plots

One-line daily quest