diff --git a/20201112/main.go b/20201112/main.go new file mode 100644 index 0000000..498a6dd --- /dev/null +++ b/20201112/main.go @@ -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 +} diff --git a/20201112/main_test.go b/20201112/main_test.go new file mode 100644 index 0000000..b117d77 --- /dev/null +++ b/20201112/main_test.go @@ -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) + } + }) + } +}