-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
129 lines (114 loc) · 2.57 KB
/
util.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
package utils
import (
"math/rand"
"time"
"strconv"
"math/big"
"errors"
)
// get pointer of object
func NewOf[T any](obj T) *T {
return &obj
}
// check object in the array or not
func IsIn[T comparable](target T, array []T) bool {
for _, element := range array {
if target == element {
return true
}
}
return false
}
// get mod of num/k
func StrMod(num string, k int) (int,error) {
left := 0
for m := 0; m < len(num); m++ {
number, err := strconv.Atoi(num[m : m+1])
if err!=nil {
return -1,err
}
left = (left*10 + number) % k
}
return left,nil
}
// get num1*num2 as string
func StrMul(numA string, numB string) (string,error) {
ba, bb := big.NewInt(0), big.NewInt(0)
if _, ok := ba.SetString(numA, 10); !ok {
return "", errors.New("invalid numA")
}
if _, ok := bb.SetString(numB, 10); !ok {
return "", errors.New("invalid numB")
}
mul := big.NewInt(0).Mul(ba,bb)
return mul.String(),nil
}
func StrAdd(numA string, numB string) (string,error) {
ba, bb := big.NewInt(0), big.NewInt(0)
if _, ok := ba.SetString(numA, 10); !ok {
return "", errors.New("invalid numA")
}
if _, ok := bb.SetString(numB, 10); !ok {
return "", errors.New("invalid numB")
}
mul := big.NewInt(0).Add(ba,bb)
return mul.String(),nil
}
func StrSub(numA string, numB string) (string,error) {
ba, bb := big.NewInt(0), big.NewInt(0)
if _, ok := ba.SetString(numA, 10); !ok {
return "", errors.New("invalid numA")
}
if _, ok := bb.SetString(numB, 10); !ok {
return "", errors.New("invalid numB")
}
mul := big.NewInt(0).Sub(ba,bb)
return mul.String(),nil
}
func StrReverse(s string) string {
rns := []rune(s)
for i, j := 0, len(rns)-1; i < j; i, j = i+1, j-1 {
rns[i], rns[j] = rns[j], rns[i]
}
return string(rns)
}
// get random element from slice
func GetRandElem[T any](count int, data []T, isCopy bool) []T {
if len(data) < count {
return nil
}
nums := data
if isCopy {
nums = make([]T, len(data))
copy(nums, data)
}
r := rand.New(rand.NewSource(time.Now().UnixNano()))
r.Shuffle(len(nums), func(i, j int) {
nums[i], nums[j] = nums[j], nums[i]
})
return nums[0:count]
}
// get distinct random numbers from [start,end)
func GetRandomNumber(start int, end int, count int) []int {
if end < start || (end-start) < count {
return nil
}
nums := make([]int, count)
i := 0
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i < count {
num := r.Intn((end - start)) + start
exist := false
for _, v := range nums {
if v == num {
exist = true
break
}
}
if !exist {
nums[i] = num
i++
}
}
return nums
}