This commit is contained in:
VicRen
2020-12-09 10:39:13 +08:00
parent 002b6eb037
commit ff3e17b7e2
2 changed files with 111 additions and 0 deletions

25
20201209/main.go Normal file
View File

@@ -0,0 +1,25 @@
package main
func main() {
}
func transform(src [][]int) [][]int {
for i := 0; i < len(src); i++ {
for j := i; j < len(src); j++ {
src[i][j], src[j][i] = src[j][i], src[i][j]
}
}
for i, l := range src {
src[i] = reverse(l)
}
return src
}
func reverse(src []int) []int {
l := len(src)
for i := 0; i < l/2; i++ {
src[i], src[l-i-1] = src[l-i-1], src[i]
}
return src
}

86
20201209/main_test.go Normal file
View File

@@ -0,0 +1,86 @@
package main
import (
"reflect"
"testing"
)
func Test_transform(t *testing.T) {
type args struct {
src [][]int
}
tests := []struct {
name string
args args
want [][]int
}{
{
"test1",
args{[][]int{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
}},
[][]int{
{7, 4, 1},
{8, 5, 2},
{9, 6, 3},
},
},
{
"test2",
args{[][]int{
{5, 1, 9, 11},
{2, 4, 8, 10},
{13, 3, 6, 7},
{15, 14, 12, 16},
}},
[][]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) {
if got := transform(tt.args.src); !reflect.DeepEqual(got, tt.want) {
t.Errorf("transform() = %v, want %v", got, tt.want)
}
})
}
}
func Test_reverse(t *testing.T) {
type args struct {
src []int
}
tests := []struct {
name string
args args
want []int
}{
{
"test1",
args{
[]int{1, 2, 3},
},
[]int{3, 2, 1},
},
{
"test2",
args{
[]int{1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3},
},
[]int{3, 2, 1, 3, 2, 1, 3, 2, 1, 3, 2, 1},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := reverse(tt.args.src); !reflect.DeepEqual(got, tt.want) {
t.Errorf("reverse() = %v, want %v", got, tt.want)
}
})
}
}