mirror of
https://github.com/cubixle/codekata-golang.git
synced 2026-04-24 19:54:43 +01:00
20201204
This commit is contained in:
37
20201204/main.go
Normal file
37
20201204/main.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"sort"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
}
|
||||
|
||||
func countMove(nums []int) int {
|
||||
count := 0
|
||||
l := len(nums)
|
||||
for {
|
||||
if isAllSame(nums) {
|
||||
return count
|
||||
}
|
||||
sort.Ints(nums)
|
||||
for i := 0; i < l-1; i++ {
|
||||
nums[i] += 1
|
||||
}
|
||||
count++
|
||||
}
|
||||
}
|
||||
|
||||
func isAllSame(nums []int) bool {
|
||||
if len(nums) == 0 {
|
||||
return false
|
||||
}
|
||||
base := nums[0]
|
||||
for _, n := range nums {
|
||||
if base != n {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
39
20201204/main_test.go
Normal file
39
20201204/main_test.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_countMove(t *testing.T) {
|
||||
type args struct {
|
||||
src []int
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want int
|
||||
}{
|
||||
{
|
||||
"test1",
|
||||
args{[]int{1, 2, 3}},
|
||||
3,
|
||||
},
|
||||
{
|
||||
"test2",
|
||||
args{[]int{1, 1, 1}},
|
||||
0,
|
||||
},
|
||||
{
|
||||
"test3",
|
||||
args{[]int{-100, 0, 100}},
|
||||
300,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := countMove(tt.args.src); got != tt.want {
|
||||
t.Errorf("countMove() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user