go-by-test/sum/sum.go

35 lines
517 B
Go
Raw Normal View History

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