arrays-slices
This commit is contained in:
parent
ef3c63d7f4
commit
cd509e9acd
32
arrays/sum.go
Normal file
32
arrays/sum.go
Normal file
@ -0,0 +1,32 @@
|
||||
package main
|
||||
|
||||
func Sum(numbers []int) int {
|
||||
sum := 0
|
||||
for _, number := range numbers {
|
||||
sum += number
|
||||
}
|
||||
return sum
|
||||
}
|
||||
|
||||
func SumAll(numbersToSum ...[]int) []int {
|
||||
var sums []int
|
||||
|
||||
for _, numbers := range numbersToSum {
|
||||
sums = append(sums, Sum(numbers))
|
||||
}
|
||||
|
||||
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
|
||||
}
|
48
arrays/sum_test.go
Normal file
48
arrays/sum_test.go
Normal file
@ -0,0 +1,48 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSum(t *testing.T) {
|
||||
t.Run("collection of 5 numbers", func(t *testing.T) {
|
||||
numbers := []int{1, 2, 3, 4, 5}
|
||||
|
||||
got := Sum(numbers)
|
||||
want := 15
|
||||
|
||||
if got != want {
|
||||
t.Errorf("got %d want %d given, %v", got, want, numbers)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestSumAll(t *testing.T) {
|
||||
got := SumAll([]int{1, 2}, []int{0, 9})
|
||||
want := []int{3, 9}
|
||||
|
||||
if !slices.Equal(got, want) {
|
||||
t.Errorf("got %v want %v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSumAllTails(t *testing.T) {
|
||||
checkSums := func(t testing.TB, got, want []int) {
|
||||
t.Helper()
|
||||
if !slices.Equal(got, want) {
|
||||
t.Errorf("got %v want %v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
t.Run("make the sums of some slices", func(t *testing.T) {
|
||||
got := SumAllTails([]int{1, 2}, []int{0, 9})
|
||||
want := []int{2, 9}
|
||||
checkSums(t, got, want)
|
||||
})
|
||||
t.Run("safely sum empty slices", func(t *testing.T) {
|
||||
got := SumAllTails([]int{}, []int{3, 4, 5})
|
||||
want := []int{0, 9}
|
||||
checkSums(t, got, want)
|
||||
})
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user