-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgo.go
114 lines (99 loc) · 2.21 KB
/
algo.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package goutils
import (
"sort"
"strings"
)
// ToTitle converts the first letter of every term in a sentence upper case.
// It's similar to strings.Title(), except it will only separate strings by space,
// so that "women's clothes" will be transformed to "Women's Clothes" instead of
// "Women'S Clothes".
func ToTitle(s string) string {
var outs []string
parts := strings.Split(s, " ")
for _, part := range parts {
if len(part) > 0 {
outs = append(outs, strings.ToUpper(part[0:1])+strings.ToLower(part[1:]))
} else {
outs = append(outs, "")
}
}
return strings.Join(outs, " ")
}
func DedupStrings(slice []string) []string {
m := map[string]bool{}
ret := []string{}
for _, s := range slice {
if s == "" {
continue
}
if m[s] {
continue
} else {
m[s] = true
ret = append(ret, s)
}
}
return ret
}
// StringSliceContains determines whether a string is contained in another slice.
// NOTE: This is just a convinient helper. It's computing time complexity is O(N), which may be a performance trap.
func StringSliceContains(slice []string, s string) bool {
for _, item := range slice {
if item == s {
return true
}
}
return false
}
func ReverseStringSlice(slice []string) []string {
ret := make([]string, len(slice))
for i := 0; i < len(slice); i++ {
ret[i] = slice[len(slice)-1-i]
}
return ret
}
func StringSliceIndex(slice []string, s string) int {
for i, e := range slice {
if e == s {
return i
}
}
return -1
}
type StringIntPair struct {
Key string
Value int
}
type OrderByValue []StringIntPair
func (slice OrderByValue) Less(i, j int) bool {
return slice[i].Value < slice[j].Value
}
func (slice OrderByValue) Swap(i, j int) {
slice[i], slice[j] = slice[j], slice[i]
}
func (slice OrderByValue) Len() int {
return len(slice)
}
func IterStringIntMap(in map[string]int) []StringIntPair {
keys := make([]string, len(in))
i := 0
for key, _ := range in {
keys[i] = key
i++
}
sort.Strings(keys)
ret := make([]StringIntPair, len(keys))
i = 0
for _, key := range keys {
ret[i] = StringIntPair{key, in[key]}
i++
}
return ret
}
func GenMask(slice []string) map[string]bool {
ret := map[string]bool{}
for _, i := range slice {
ret[i] = true
}
return ret
}