This commit is contained in:
VicRen
2020-11-12 09:21:20 +08:00
parent 6fad915012
commit 6044d6790c
2 changed files with 107 additions and 0 deletions

31
20201112/main.go Normal file
View File

@@ -0,0 +1,31 @@
package main
func main() {
}
func sortFor(n int, src []int) []int {
var find []int
for _, x := range src {
if x == n {
find = append(find, x)
} else {
left = append(left, x)
}
}
return append(find, left...)
}
func sortFor2(n int, src []int) []int {
l := len(src)
for i := l - 1; i >= 0; i-- {
for j := l - 1; j >= 0; j-- {
if src[i] != n && src[j] == n {
tmp := src[i]
src[i] = src[j]
src[j] = tmp
}
}
}
return src
}

76
20201112/main_test.go Normal file
View File

@@ -0,0 +1,76 @@
package main
import (
"reflect"
"testing"
)
func Test_sortFor(t *testing.T) {
type args struct {
n int
src []int
}
tests := []struct {
name string
args args
want []int
}{
{
"example_1",
args{1, []int{5, 1, 6, 1}},
[]int{1, 1, 5, 6},
},
{
"example_2",
args{2, []int{-1, 2, 3, 5, 2, 2}},
[]int{2, 2, 2, -1, 3, 5},
},
{
"example_3",
args{1, []int{2, 3, 4, 6}},
[]int{2, 3, 4, 6},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := sortFor(tt.args.n, tt.args.src); !reflect.DeepEqual(got, tt.want) {
t.Errorf("sortFor() = %v, want %v", got, tt.want)
}
})
}
}
func Test_sortFor2(t *testing.T) {
type args struct {
n int
src []int
}
tests := []struct {
name string
args args
want []int
}{
{
"example_1",
args{1, []int{5, 1, 6, 1}},
[]int{1, 1, 5, 6},
},
{
"example_2",
args{2, []int{-1, 2, 3, 5, 2, 2}},
[]int{2, 2, 2, -1, 3, 5},
},
{
"example_3",
args{1, []int{2, 3, 4, 6}},
[]int{2, 3, 4, 6},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := sortFor2(tt.args.n, tt.args.src); !reflect.DeepEqual(got, tt.want) {
t.Errorf("sortFor2() = %v, want %v", got, tt.want)
}
})
}
}