/
stringlist.go
259 lines (224 loc) · 5.8 KB
/
stringlist.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package core
import (
"fmt"
"math/bits"
"sort"
"strings"
)
// AppendString :
// implements append
func AppendString(array []string, element string) []string {
newArray := append(array, element)
return newArray
}
// IndexOfString :
// returns index of element. returns -1 if element dont exist
func IndexOfString(array *[]string, element string) int {
for index, value := range *array {
if value == element {
return index
}
}
return -1
}
// LastString :
// return last element in slice
func LastString(array []string) (string, error) {
if len(array) == 0 {
return "", ErrListEmpty
}
return array[len(array)-1], nil
}
// SortString :
// sorts slice
func SortString(list *[]string, reverse bool) *[]string {
if reverse {
sort.SliceStable(*list, func(i, j int) bool {
return (*list)[i] > (*list)[j]
})
} else {
sort.SliceStable(*list, func(i, j int) bool {
return (*list)[i] < (*list)[j]
})
}
return list
}
// PopString :
// removes element from slice
func PopString(list *[]string, index int) ([]string, string) {
listD := *list
popped := listD[index]
newArray := append(listD[:index], listD[index+1:]...)
return newArray, popped
}
// CountString :
// returns the number of times a given element appears in a slice
func CountString(list *[]string, element string) int {
count := 0
for _, value := range *list {
if value == element {
count++
}
}
return count
}
// ExtendString :
// adds two slice together
func ExtendString(list *[]string, another []string) []string {
return append((*list), another...)
}
// InsertString :
// insert an element in a given position
func InsertString(list *[]string, element string, index int) *[]string {
postpend := append([]string{element}, (*list)[index:]...)
newList := append((*list)[:index], postpend...)
return &newList
}
// RemoveString :
// removes given element from slice
func RemoveString(list *[]string, element string) (*[]string, error) {
index := IndexOfString(list, element)
if index == -1 {
return nil, ErrNotInList
}
newList, _ := PopString(list, index)
return &newList, nil
}
// ReverseString :
// reverse the positions of elements in slice
func ReverseString(list *[]string) *[]string {
newList := []string{}
for i := len(*list) - 1; i >= 0; i-- {
newList = append(newList, (*list)[i])
}
return &newList
}
// SumString :
// joins elements in slice
func SumString(list *[]string) (sum string) {
sum = strings.Join(*list, " ")
return
}
// MaxString :
// returns max element in slice
func MaxString(list *[]string) (max string) {
for i, value := range *list {
if i == 0 || value > max {
max = value
}
}
return
}
// MinString :
// returns min element in slice
func MinString(list *[]string) (min string) {
for i, value := range *list {
if i == 0 || value < min {
min = value
}
}
return
}
// CombinationsString :
// adapted from https://github.com/mxschmitt/golang-combinations
func CombinationsString(set []string, n int, joiner string) (subsets []string) {
length := uint(len(set))
if n > len(set) {
n = len(set)
}
// Go through all possible combinations of objects
// from 1 (only first object in subset) to 2^length (all objects in subset)
for subsetBits := 1; subsetBits < (1 << length); subsetBits++ {
if n > 0 && bits.OnesCount(uint(subsetBits)) != n {
continue
}
var subset string
for object := uint(0); object < length; object++ {
// checks if object is contained in subset
// by checking if bit 'object' is set in subsetBits
if (subsetBits>>object)&1 == 1 {
// add object to subset
subset += set[object] + joiner
}
}
// remove unwanted joiner and add subset to subsets
if len(joiner) != 0 {
subset = subset[:len(subset)-1]
}
subsets = append(subsets, subset)
}
return subsets
}
// CombinationsStringMax returns combinations of max length.
// For example: a list of [a, b, c] with max combination 2, should return 1 and 2 combinations.
// i.e combinations length <= n
// adapted from https://github.com/mxschmitt/golang-combinations
func CombinationsStringMax(set []string, n int, joiner string) (subsets []string) {
length := uint(len(set))
if n > len(set) {
n = len(set)
}
// Go through all possible combinations of objects
// from 1 (only first object in subset) to 2^length (all objects in subset)
for subsetBits := 1; subsetBits < (1 << length); subsetBits++ {
if n > 0 && bits.OnesCount(uint(subsetBits)) > n {
continue
}
var subset string
for object := uint(0); object < length; object++ {
// checks if object is contained in subset
// by checking if bit 'object' is set in subsetBits
if (subsetBits>>object)&1 == 1 {
// add object to subset
subset += set[object] + joiner
}
}
// remove unwanted joiner and add subset to subsets
if len(joiner) != 0 {
subset = subset[:len(subset)-1]
}
subsets = append(subsets, subset)
}
return subsets
}
// SetString returns a set of slice i.e removes duplicates
func SetString(list []string) (set []string) {
keys := map[string]bool{}
for _, key := range list {
if !keys[key] {
keys[key] = true
set = append(set, key)
}
}
return
}
// ConvertToString converts to a specified list type
func ConvertToString(array interface{}) (stringfy []string, err error) {
switch array := array.(type) {
case []int:
for _, v := range array {
stringfy = append(stringfy, fmt.Sprintf("%v", v))
}
case []int32:
for _, v := range array {
stringfy = append(stringfy, fmt.Sprintf("%v", v))
}
case []int64:
for _, v := range array {
stringfy = append(stringfy, fmt.Sprintf("%v", v))
}
case []float32:
for _, v := range array {
stringfy = append(stringfy, fmt.Sprintf("%v", v))
}
case []float64:
for _, v := range array {
stringfy = append(stringfy, fmt.Sprintf("%v", v))
}
case []string:
return array, nil
default:
return nil, ErrTypeNotSupported
}
return
}