SumAllTails implemented, refactor tests
This commit is contained in:
@@ -18,3 +18,18 @@ func SumAll(numbersToSum ...[]int) []int {
|
|||||||
|
|
||||||
return sums
|
return sums
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func SumAllTails(numbersToSum ...[]int) []int {
|
||||||
|
var sums []int
|
||||||
|
|
||||||
|
for _, numbers := range numbersToSum {
|
||||||
|
if len(numbers) == 0 {
|
||||||
|
sums = append(sums, 0)
|
||||||
|
} else {
|
||||||
|
tail := numbers[1:]
|
||||||
|
sums = append(sums, Sum(tail))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return sums
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"reflect"
|
||||||
"slices"
|
"slices"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
@@ -37,3 +38,23 @@ func TestSumAll(t *testing.T) {
|
|||||||
t.Errorf("got %d want %v", got, want)
|
t.Errorf("got %d want %v", got, want)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSumAllTails(t *testing.T) {
|
||||||
|
checkSums := func(t testing.TB, got, want []int) {
|
||||||
|
t.Helper()
|
||||||
|
if !reflect.DeepEqual(got, want) {
|
||||||
|
t.Errorf("got %v want %v", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Run("sums of slices", func(t *testing.T) {
|
||||||
|
got := SumAllTails([]int{1, 2}, []int{0, 9})
|
||||||
|
want := []int{2, 9}
|
||||||
|
checkSums(t, got, want)
|
||||||
|
})
|
||||||
|
t.Run("sums with some empty slices", func(t *testing.T) {
|
||||||
|
got := SumAllTails([]int{}, []int{3, 4, 5})
|
||||||
|
want := []int{0, 9}
|
||||||
|
checkSums(t, got, want)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user