diff --git a/20201113/main.go b/20201113/main.go new file mode 100644 index 0000000..57f9e0a --- /dev/null +++ b/20201113/main.go @@ -0,0 +1,25 @@ +package main + +import "fmt" + +func main() { + fmt.Println(isCompliance([]int{2, 3, 3, 2, 4})) + fmt.Println(isCompliance([]int{3, 3, 2, 4})) + fmt.Println(isCompliance([]int{4, 5, 1, 2})) +} + +func isCompliance(nums []int) bool { + sc := 0 + for i := 0; i < len(nums); i++ { + for j := 1; j < len(nums)-i; j++ { + if nums[j] < nums[j-1] { + if sc > 1 { + return false + } + sc++ + nums[j], nums[j-1] = nums[j-1], nums[j] + } + } + } + return true +} diff --git a/20201113/main_test.go b/20201113/main_test.go new file mode 100644 index 0000000..7742528 --- /dev/null +++ b/20201113/main_test.go @@ -0,0 +1,32 @@ +package main + +import "testing" + +func Test_isCompliance(t *testing.T) { + type args struct { + nums []int + } + tests := []struct { + name string + args args + want bool + }{ + { + "example_1", + args{[]int{4, 2, 3}}, + true, + }, + { + "example_2", + args{[]int{4, 2, 1}}, + false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := isCompliance(tt.args.nums); got != tt.want { + t.Errorf("isCompliance() = %v, want %v", got, tt.want) + } + }) + } +}