Drill

ProblemsGo › patterns

Turn the floor plan a quarter turn

mediumpatternsGridsArraysGo

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

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

Go 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.

Solve it in Python →

Where you start

func rotateClockwise(plan [][]int) [][]int {
	
}

Worked examples

CallResult
rotateClockwise([][]int{[]int{1, 2}, []int{3, 4}})[][]int{[]int{3, 1}, []int{4, 2}}
rotateClockwise([][]int{[]int{1, 2, 3}, []int{4, 5, 6}, []int{7, 8, 9}})[][]int{[]int{7, 4, 1}, []int{8, 5, 2}, []int{9, 6, 3}}
rotateClockwise([][]int{[]int{1}})[][]int{[]int{1}}
rotateClockwise([][]int{})[][]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 Go
func rotateClockwise(plan [][]int) [][]int {
	n := len(plan)
	turned := [][]int{}
	for r := 0; r < n; r++ {
	    row := []int{}
	    for c := 0; c < n; c++ {
	        row = append(row, plan[n-1-c][r])
	    }
	    turned = append(turned, row)
	}
	return turned
}

The same problem in another language

More patterns problems in Go