mirror of
https://github.com/cubixle/codekata-golang.git
synced 2026-04-24 23:04:42 +01:00
20201208
This commit is contained in:
31
20201208/main.go
Normal file
31
20201208/main.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
}
|
||||
|
||||
func canSeparate(src []int) bool {
|
||||
//A[0] + A[1] + ... + A[i] == A[i+1] + A[i+2] + ... + A[j-1] == A[j] + A[j-1] + ... + A[A.length - 1]
|
||||
l := len(src)
|
||||
for i := 1; i < l-1; i++ {
|
||||
for j := i + 1; j < l; j++ {
|
||||
s1 := sumSlice(src[:i])
|
||||
s2 := sumSlice(src[i:j])
|
||||
s3 := sumSlice(src[j:])
|
||||
if s1 == s2 && s2 == s3 {
|
||||
fmt.Println("separated on: ", i, j)
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func sumSlice(src []int) int {
|
||||
ret := 0
|
||||
for _, n := range src {
|
||||
ret += n
|
||||
}
|
||||
return ret
|
||||
}
|
||||
90
20201208/main_test.go
Normal file
90
20201208/main_test.go
Normal file
@@ -0,0 +1,90 @@
|
||||
package main
|
||||
|
||||
import "testing"
|
||||
|
||||
func Test_canSeparate(t *testing.T) {
|
||||
type args struct {
|
||||
src []int
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want bool
|
||||
}{
|
||||
{
|
||||
"test1",
|
||||
args{
|
||||
[]int{1, 1, 1},
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"test1",
|
||||
args{
|
||||
[]int{0, 2, 1, -6, 6, -7, 9, 1, 2, 0, 1},
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"test2",
|
||||
args{
|
||||
[]int{0, 2, 1, -6, 6, 7, 9, -1, 2, 0, 1},
|
||||
},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"test3",
|
||||
args{
|
||||
[]int{3, 3, 6, 5, -2, 2, 5, 1, -9, 4},
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"test4",
|
||||
args{
|
||||
[]int{10, -10, 10, -10, 10, -10, 10, -10},
|
||||
},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"test5",
|
||||
args{
|
||||
[]int{1, -1, 1, -1},
|
||||
},
|
||||
false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := canSeparate(tt.args.src); got != tt.want {
|
||||
t.Errorf("canSeparate() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_sumSlice(t *testing.T) {
|
||||
type args struct {
|
||||
src []int
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want int
|
||||
}{
|
||||
{
|
||||
"test1",
|
||||
args{
|
||||
[]int{1, 2},
|
||||
},
|
||||
3,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := sumSlice(tt.args.src); got != tt.want {
|
||||
t.Errorf("sumSlice() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user