Turn the floor plan a quarter turn
A layout tool rotates a square plan ninety degrees clockwise so it fits the room the other way round.
- The plan is square.
- The top row becomes the right-hand column, read downwards.
- An empty plan comes back empty.
rotate_clockwise(plan: list<list<int>>) → list<list<int>>
Where you start
def rotate_clockwise(plan: list[list[int]]) -> list[list[int]]:
Worked examples
| Call | Result |
|---|---|
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