-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfrequency.go
78 lines (60 loc) · 1.24 KB
/
frequency.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
71
72
73
74
75
76
77
78
package li
import (
"fmt"
"regexp"
"sort"
"strconv"
)
var uniqCRegexp = regexp.MustCompile(`^\s*(\d+)\s+(.*)$`)
type Frequency struct {
Item string
Count int
}
// Frequencies is a slice of item/count pairs, must be sorted by count
type Frequencies []Frequency
func (v Frequencies) maxCount() (c int) {
for _, frequency := range v {
if frequency.Count > c {
c = frequency.Count
}
}
return
}
func (v Frequencies) Len() int {
return len(v)
}
func (v Frequencies) Swap(i, j int) {
v[i], v[j] = v[j], v[i]
}
func (v Frequencies) Less(i, j int) bool {
if v[i].Count == v[j].Count {
return v[i].Item > v[j].Item
}
return v[i].Count > v[j].Count
}
func NewFrequencies(lines []string) (Frequencies, error) {
fr := make(Frequencies, 0, len(lines))
for _, line := range lines {
f, err := newFrequency(line)
if err != nil {
return nil, err
}
fr = append(fr, f)
}
sort.Sort(fr)
return fr, nil
}
func newFrequency(line string) (Frequency, error) {
m := uniqCRegexp.FindStringSubmatch(line)
if len(m) != 3 {
return Frequency{}, fmt.Errorf("failed to parse %s: %+v", line, m)
}
count, err := strconv.Atoi(m[1])
if err != nil {
return Frequency{}, err
}
return Frequency{
Item: m[2],
Count: count,
}, nil
}