go-by-test/sum/sum.go

35 lines
517 B
Go
Raw Normal View History

2024-09-10 21:15:12 +02:00
package sum
2024-09-10 21:27:13 +02:00
func Sum(numbers []int) int {
2024-09-10 21:15:12 +02:00
sum := 0
2024-09-10 21:16:01 +02:00
for _, number := range numbers {
sum += number
2024-09-10 21:15:12 +02:00
}
return sum
}
2024-09-10 21:34:27 +02:00
2024-09-10 21:38:18 +02:00
func SumAll(numbersToSum ...[]int) []int {
res := make([]int, len(numbersToSum))
for i, numbers := range numbersToSum {
res[i] = Sum(numbers)
}
return res
2024-09-10 21:34:27 +02:00
}
2024-09-10 21:51:21 +02:00
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
}