go-by-test/sum/sum.go
2024-09-10 21:51:21 +02:00

35 lines
517 B
Go

package sum
func Sum(numbers []int) int {
sum := 0
for _, number := range numbers {
sum += number
}
return sum
}
func SumAll(numbersToSum ...[]int) []int {
res := make([]int, len(numbersToSum))
for i, numbers := range numbersToSum {
res[i] = Sum(numbers)
}
return res
}
func SumAllTails(numbersToSum ...[]int) []int {
res := make([]int, len(numbersToSum))
for i, numbers := range numbersToSum {
if len(numbers) == 0 {
res[i] = 0
continue
}
res[i] = Sum(numbers[1:])
}
return res
}