This commit is contained in:
VicRen
2020-12-30 09:20:26 +08:00
parent 6ed6173f36
commit bef6df88ed
2 changed files with 120 additions and 0 deletions

36
20201230/main.go Normal file
View File

@@ -0,0 +1,36 @@
package main
func main() {
}
func process(nums []int) []int {
var ret []int
c := 0
for {
if len(ret) == len(nums) {
break
}
n := findNextLarge(c, nums)
ret = append(ret, n)
c++
}
return ret
}
func findNextLarge(index int, nums []int) int {
if index > len(nums)-1 {
return -1
}
src := append(nums[index+1:], nums[:index+1]...)
for i, c := range src {
if c > nums[index] {
x := index + i + 1
if x > len(nums) {
x -= len(nums)
}
return c
}
}
return -1
}

84
20201230/main_test.go Normal file
View File

@@ -0,0 +1,84 @@
package main
import (
"reflect"
"testing"
)
func Test_process(t *testing.T) {
type args struct {
nums []int
}
tests := []struct {
name string
args args
want []int
}{
{
"test1",
args{
nums: []int{1, 2, 1},
},
[]int{2, -1, 2},
},
{
"test2",
args{
nums: []int{1},
},
[]int{-1},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := process(tt.args.nums); !reflect.DeepEqual(got, tt.want) {
t.Errorf("process() = %v, want %v", got, tt.want)
}
})
}
}
func Test_findNextLarge(t *testing.T) {
type args struct {
index int
nums []int
}
tests := []struct {
name string
args args
want int
}{
{
"test1",
args{
1,
[]int{1, 2, 3},
},
3,
},
{
"test2",
args{
0,
[]int{1, 2, 3},
},
2,
},
{
"test3",
args{
2,
[]int{1, 2, 3},
},
-1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := findNextLarge(tt.args.index, tt.args.nums)
if got != tt.want {
t.Errorf("findNextLarge() got = %v, want %v", got, tt.want)
}
})
}
}