-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers_numbers.go
61 lines (47 loc) · 988 Bytes
/
helpers_numbers.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
package epochid
import (
"crypto/rand"
"fmt"
"strconv"
"strings"
)
func pickNumbersFrom(s string, howMany uint) int64 {
isNumber := func(letter string) bool {
numbers := []string{
"1", "2", "3", "4", "5", "6", "7", "8", "9",
}
for _, number := range numbers {
if letter == number {
return true
}
}
return false
}
var j int
res := make([]string, 0)
for i := len(s); i >= 0; i-- {
if isNumber(s[i-1 : i]) {
res = append(res, s[i-1:i])
j++
}
if j == int(howMany) {
result, _ := strconv.Atoi(
strings.Join(res, ""),
)
return int64(result)
}
}
return 0
}
// randInt generates a random uint32 as 4 digit string
// time cost is around 500 ns.
func randInt() (string, error) {
buf := make([]byte, 3)
if _, err := rand.Reader.Read(buf); err != nil {
return "",
fmt.Errorf("generate random number: %w;", err)
}
res := uint32(buf[0])<<16 | uint32(buf[1])<<8 | uint32(buf[2])
return strconv.Itoa(int(res)),
nil
}