-
Notifications
You must be signed in to change notification settings - Fork 0
/
calc.go
70 lines (61 loc) · 1.28 KB
/
calc.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
60
61
62
63
64
65
66
67
68
69
70
// Copyright 2018 mikan.
package chartgen
import (
"math"
"sort"
)
type Entry struct {
Name string
Value int
}
type OrderedEntries []Entry
func (l OrderedEntries) Len() int {
return len(l)
}
func (l OrderedEntries) Swap(i, j int) {
l[i], l[j] = l[j], l[i]
}
func (l OrderedEntries) Less(i, j int) bool {
if l[i].Value == l[j].Value {
return l[i].Name < l[j].Name
} else {
return l[i].Value > l[j].Value // desc
}
}
type Ranking struct {
List []Entry
Max int
Min int
Total int
}
func AttendeeRanking(records []Record) Ranking {
maxAttends := math.MinInt64
minAttends := math.MaxInt64
totalAttends := 0
attendees := make(map[string]int)
for _, record := range records {
totalAttends += record.AttendsTotal
if record.AttendsTotal > maxAttends {
maxAttends = record.AttendsTotal
}
if record.AttendsTotal < minAttends {
minAttends = record.AttendsTotal
}
for _, attendee := range record.Attends {
attendees[attendee]++
}
}
if totalAttends == 0 {
return Ranking{[]Entry{}, 0, 0, 0}
}
return Ranking{desc(attendees), maxAttends, minAttends, totalAttends}
}
func desc(attendees map[string]int) []Entry {
ranking := OrderedEntries{}
for k, v := range attendees {
e := Entry{k, v}
ranking = append(ranking, e)
}
sort.Sort(ranking)
return ranking
}