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.
RotateClockwise(plan: list<list<int>>) → list<list<int>>
C# needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.
Where you start
public List<List<int>> RotateClockwise(List<List<int>> plan) {
}
Worked examples
| Call | Result |
|---|---|
RotateClockwise(new List<List<int>> { new List<int> { 1, 2 }, new List<int> { 3, 4 } }) | new List<List<int>> { new List<int> { 3, 1 }, new List<int> { 4, 2 } } |
RotateClockwise(new List<List<int>> { new List<int> { 1, 2, 3 }, new List<int> { 4, 5, 6 }, new List<int> { 7, 8, 9 } }) | new List<List<int>> { new List<int> { 7, 4, 1 }, new List<int> { 8, 5, 2 }, new List<int> { 9, 6, 3 } } |
RotateClockwise(new List<List<int>> { new List<int> { 1 } }) | new List<List<int>> { new List<int> { 1 } } |
RotateClockwise(new List<List<int>> { }) | new List<List<int>> { } |
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 C#
public List<List<int>> RotateClockwise(List<List<int>> plan) {
int n = plan.Count;
var turned = new List<List<int>>();
for (int r = 0; r < n; r++) {
var row = new List<int>();
for (int c = 0; c < n; c++) row.Add(plan[n - 1 - c][r]);
turned.Add(row);
}
return turned;
}