-
Notifications
You must be signed in to change notification settings - Fork 0
/
average.go
37 lines (32 loc) · 860 Bytes
/
average.go
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
// Provide some performance metrics. Everything is computed based on integer
// arithmetic, what's all this float stuff, anyhow?
// http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
// Early Forth did not include floating point arithmetic, and they did like
// control telescopes.
package main
import (
"sort"
)
// ArithmeticMean does not perform any bound or overflow checking
func ArithmeticMean(is []int) int {
sum := 0
for _, v := range is {
sum += v
}
return sum / len(is)
}
// Median returns median average of values
func Median(slice []int) int {
// sorting is in-place, create copy so that input is not mutated
is := append([]int(nil), slice...)
sort.Ints(is)
n := len(is)
if even(n) {
upper := n / 2
return ArithmeticMean([]int{is[upper-1], is[upper]})
}
return is[n/2]
}
func even(i int) bool {
return i%2 == 0
}