Drill

ProblemsPython › patterns

Turn the floor plan a quarter turn

mediumpatternsGridsArraysPython

A layout tool rotates a square plan ninety degrees clockwise so it fits the room the other way round.

rotate_clockwise(plan: list<list<int>>) → list<list<int>>

Solve it in the editor →

Where you start

def rotate_clockwise(plan: list[list[int]]) -> list[list[int]]:
    

Worked examples

CallResult
rotate_clockwise([[1, 2], [3, 4]])[[3, 1], [4, 2]]
rotate_clockwise([[1, 2, 3], [4, 5, 6], [7, 8, 9]])[[7, 4, 1], [8, 5, 2], [9, 6, 3]]
rotate_clockwise([[1]])[[1]]
rotate_clockwise([])[]

Hint

The cell at row r, column c lands at row c, column (last - r). Building a fresh grid is easier to get right than shuffling in place.

Reference solution in Python
def rotate_clockwise(plan: list[list[int]]) -> list[list[int]]:
    n = len(plan)
    turned = []
    for r in range(n):
        turned.append([plan[n - 1 - c][r] for c in range(n)])
    return turned

The same problem in another language

More patterns problems in Python