-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsum.go
More file actions
38 lines (31 loc) · 757 Bytes
/
sum.go
File metadata and controls
38 lines (31 loc) · 757 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package main
// Sum returns the total of an array of numbers.
func Sum(numbers []int) int {
sum := 0
for _, number := range numbers {
sum += number
}
return sum
}
// SumAll takes a varying number of slices, returning a new slice containing
// the totals of each slice passed in.
func SumAll(numbersToSum ...[]int) []int {
var sums []int
for _, numbers := range numbersToSum {
sums = append(sums, Sum(numbers))
}
return sums
}
// SumAllTails calculates the totals of the "tails" of each slice.
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
}