Skip to content

Commit

Permalink
Merge pull request #73 from arunsathiya/solve/48-rotate-image
Browse files Browse the repository at this point in the history
48. Rotate Image
  • Loading branch information
arunsathiya committed Feb 25, 2024
2 parents 97ff08e + 644d973 commit e905565
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/48-rotate-image/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

func rotate(matrix [][]int) {
n := len(matrix)
for i := 0; i < n; i++ {
for j := i; j < n; j++ {
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
}
for j, k := 0, len(matrix[0])-1; j < k; j, k = j+1, k-1 {
matrix[i][j], matrix[i][k] = matrix[i][k], matrix[i][j]
}
}
}
34 changes: 34 additions & 0 deletions src/48-rotate-image/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package main

import (
"reflect"
"testing"
)

func TestRotate(t *testing.T) {
tests := []struct {
name string
matrix [][]int
want [][]int
}{
{
name: "Example 1",
matrix: [][]int{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}},
want: [][]int{{7, 4, 1}, {8, 5, 2}, {9, 6, 3}},
},
{
name: "Example 2",
matrix: [][]int{{5, 1, 9, 11}, {2, 4, 8, 10}, {13, 3, 6, 7}, {15, 14, 12, 16}},
want: [][]int{{15, 13, 2, 5}, {14, 3, 4, 1}, {12, 6, 8, 9}, {16, 7, 10, 11}},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
rotate(tt.matrix)
if !reflect.DeepEqual(tt.matrix, tt.want) {
t.Errorf("rotate() got = %v, want %v", tt.matrix, tt.want)
}
})
}
}

0 comments on commit e905565

Please sign in to comment.