-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
59 lines (48 loc) · 1.06 KB
/
util.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main
import (
"math/rand"
"slices"
)
// var logger = slog.New(slog.NewTextHandler(os.Stdout, nil))
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890")
type Scale []int
var scales = map[string]Scale{
"fibonacci": {1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144},
"workingdays": {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14},
}
func randSeq(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}
func average(s []int) float64 {
average := 0.0
for _, v := range s {
average += float64(v)
}
return average / float64(len(s))
}
func median(s []int) float64 {
sCopy := make([]int, len(s))
copy(sCopy, s)
slices.Sort(sCopy)
l := len(sCopy)
if l == 0 {
return 0
} else if l%2 == 0 {
return average(sCopy[l/2-1 : l/2+1])
} else {
return float64(sCopy[l/2])
}
}
func recommendation(av float64, med float64, scale Scale) int {
a := (av + med) / 2
for _, entry := range scale {
if float64(entry) >= a {
return entry
}
}
return scale[len(scale)-1]
}