-
Notifications
You must be signed in to change notification settings - Fork 0
/
weight_for_weight.go
78 lines (61 loc) · 1.37 KB
/
weight_for_weight.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 weight_for_weight
import (
"fmt"
"sort"
"strconv"
"strings"
)
func OrderWeight(s string) string {
weights := newFFCWeightSlice(s)
sort.Sort(weights)
return weights.String()
}
type ffcWeight int
func newFFCWeight(s string) ffcWeight {
i, err := strconv.Atoi(s)
if err != nil {
panic(fmt.Sprintf("convert %s to integer: %v", s, err))
}
return ffcWeight(i)
}
func (w ffcWeight) String() string {
return strconv.Itoa(int(w))
}
func (w ffcWeight) Weight() int {
var sum int
for _, r := range w.String() {
i, err := strconv.Atoi(string(r))
if err != nil {
panic(fmt.Sprintf("convert %s in %d to string: %v", string(r), int(w), err))
}
sum += i
}
return sum
}
type ffcWeightSlice []ffcWeight
func newFFCWeightSlice(s string) ffcWeightSlice {
var weights ffcWeightSlice
if len(strings.TrimSpace(s)) == 0 {
return weights
}
for _, s := range strings.Split(s, " ") {
weights = append(weights, newFFCWeight(s))
}
return weights
}
func (s ffcWeightSlice) Len() int { return len(s) }
func (s ffcWeightSlice) Less(i, j int) bool {
x, y := s[i].Weight(), s[j].Weight()
if x == y {
return s[i].String() < s[j].String()
}
return x < y
}
func (s ffcWeightSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s ffcWeightSlice) String() string {
var ss []string
for _, w := range s {
ss = append(ss, w.String())
}
return strings.Join(ss, " ")
}