Editor

Rotate the Grid

#97 · Lists · Intermediate · 75 XP · about 12 min

A square grid is stored as a list of rows. Write rotate_grid(grid) that turns it 90 degrees clockwise: the left column, read from bottom to top, becomes the top row.

Change grid itself instead of building a new one: after the call, the same list must hold the rotated grid. The function does not need to return anything.

Examples

grid = [[1, 2], [3, 4]]
rotate_grid(grid)
print(grid)  # [[3, 1], [4, 2]]

grid = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
rotate_grid(grid)
print(grid)  # [[7, 4, 1], [8, 5, 2], [9, 6, 3]]

In the second example the left column 7, 4, 1 (read upwards) becomes the top row.

Constraints

  • 0 <= n <= 100, where the grid has n rows of n items

Ready?

Accept the challenge to start writing. A small timer counts the time this page is in view.

Output appears here.