-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
51 lines (43 loc) · 892 Bytes
/
utils.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
package main
import (
"reflect"
"strings"
)
// any has complexity: O(n^2)
func any(a interface{}, b interface{}) bool {
av := reflect.ValueOf(a)
for i := 0; i < av.Len(); i++ {
el := av.Index(i).Interface()
if contains(b, el) {
return true
}
}
return false
}
func contains(a interface{}, e interface{}) bool {
v := reflect.ValueOf(a)
for i := 0; i < v.Len(); i++ {
if v.Index(i).Interface() == e {
return true
}
}
return false
}
func firstWords(value string, count int) string {
// Loop over all indexes in the string.
for i := range value {
// If we encounter a space, reduce the count.
if value[i] == ' ' {
count -= 1
// When no more words required, return a substring.
if count == 0 {
return value[0:i]
}
}
}
// Return the entire string.
return value
}
func lastWord(s string) string {
return s[strings.LastIndex(s, " ")+1:]
}