Skip to content

Commit c0b34be

Browse files
committed
splitaddbothsidesarraytogether with test
1 parent 0f49e45 commit c0b34be

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
func sumArr(short, long []int) []int {
8+
s := []int{}
9+
longrev := []int{}
10+
if len(long) > len(short) {
11+
s = append(s, long[0])
12+
longrev = long[1:]
13+
} else {
14+
longrev = long[:]
15+
}
16+
for i := 0; i < len(short); i++ {
17+
s = append(s, short[i]+longrev[i])
18+
}
19+
return s
20+
}
21+
22+
// SplitAndAdd is a function
23+
func SplitAndAdd(numbers []int, n int) []int {
24+
if len(numbers) <= 1 || n == 0 {
25+
return numbers
26+
}
27+
28+
for {
29+
n1 := numbers[0 : len(numbers)/2]
30+
n2 := numbers[len(numbers)/2:]
31+
numbers = []int{}
32+
numbers = sumArr(n1, n2)
33+
n--
34+
if n == 0 || len(numbers) == 1 {
35+
break
36+
}
37+
}
38+
return numbers
39+
}
40+
41+
func main() {
42+
43+
a := SplitAndAdd([]int{1, 2, 3, 4}, 1)
44+
fmt.Println("end: ", a)
45+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package main
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func TestSplitAndAdd(t *testing.T) {
9+
s := SplitAndAdd([]int{1, 2, 3, 4, 5}, 2)
10+
if !reflect.DeepEqual(s, []int{5, 10}) {
11+
t.Errorf("SplitAndAdd was incorrect, got: %d, want %d.", s, []int{5, 10})
12+
}
13+
14+
s = SplitAndAdd([]int{15}, 3)
15+
if !reflect.DeepEqual(s, []int{15}) {
16+
t.Errorf("SplitAndAdd was incorrect, got: %d, want %d.", s, []int{15})
17+
}
18+
s = SplitAndAdd([]int{1, 2, 3, 4}, 1)
19+
if !reflect.DeepEqual(s, []int{4, 6}) {
20+
t.Errorf("SplitAndAdd was incorrect, got: %d, want %d.", s, []int{4, 6})
21+
}
22+
s = SplitAndAdd([]int{1, 2, 3, 4, 5, 6}, 20)
23+
if !reflect.DeepEqual(s, []int{21}) {
24+
t.Errorf("SplitAndAdd was incorrect, got: %d, want %d.", s, []int{21})
25+
}
26+
s = SplitAndAdd([]int{32, 45, 43, 23, 54, 23, 54, 34}, 2)
27+
if !reflect.DeepEqual(s, []int{183, 125}) {
28+
t.Errorf("SplitAndAdd was incorrect, got: %d, want %d.", s, []int{183, 125})
29+
}
30+
s = SplitAndAdd([]int{32, 45, 43, 23, 54, 23, 54, 34}, 0)
31+
if !reflect.DeepEqual(s, []int{32, 45, 43, 23, 54, 23, 54, 34}) {
32+
t.Errorf("SplitAndAdd was incorrect, got: %d, want %d.", s, []int{32, 45, 43, 23, 54, 23, 54, 34})
33+
}
34+
// s = SplitAndAdd([]int{3, 234, 25, 345, 45, 34, 234, 235, 345}, 3)
35+
// if !reflect.DeepEqual(s, []int{305, 1195}) {
36+
// t.Errorf("SplitAndAdd was incorrect, got: %d, want %d.", s, []int{305, 1195})
37+
// }
38+
// s = SplitAndAdd([]int{3, 234, 25, 345, 45, 34, 234, 235, 345, 34, 534, 45, 645, 645, 645, 4656, 45, 3}, 4)
39+
// if !reflect.DeepEqual(s, []int{1040, 7712}) {
40+
// t.Errorf("SplitAndAdd was incorrect, got: %d, want %d.", s, []int{1040, 7712})
41+
// }
42+
// s = SplitAndAdd([]int{23, 345, 345, 345, 34536, 567, 568, 6, 34536, 54, 7546, 456}, 20)
43+
// if !reflect.DeepEqual(s, []int{79327}) {
44+
// t.Errorf("SplitAndAdd was incorrect, got: %d, want %d.", s, []int{79327})
45+
// }
46+
}

0 commit comments

Comments
 (0)