-
Notifications
You must be signed in to change notification settings - Fork 18
/
Integer.go
286 lines (258 loc) · 5.41 KB
/
Integer.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package I
// Integer number support package
import (
"strconv"
"strings"
)
// If simplified ternary operator (bool ? val : 0), returns second argument, if the condition (first arg) is true, returns 0 if not
//
// I.If(true,3) // 3
// I.If(false,3) // 0
func If(b bool, yes int64) int64 {
if b {
return yes
}
return 0
}
// IfElse ternary operator (bool ? val1 : val2), returns second argument if the condition (first arg) is true, third argument if not
//
// I.IfElse(true,3,4) // 3
// I.IfElse(false,3,4) // 4
func IfElse(b bool, yes, no int64) int64 {
if b {
return yes
}
return no
}
// IfZero simplified ternary operator (bool ? val1==0 : val2), returns second argument, if val1 (first arg) is zero, returns val2 if not
//
// I.IfZero(0,3) // 3
// I.IfZero(4,3) // 4
func IfZero(val1, val2 int64) int64 {
if val1 == 0 {
return val2
}
return val1
}
// IsZero simplified ternary operator (bool ? val1==0 : val2), returns second argument, if val1 (first arg) is zero, returns val2 if not
//
// I.IsZero(0,3) // 3
// I.IsZero(4,3) // 4
func IsZero(val1, val2 int) int {
if val1 == 0 {
return val2
}
return val1
}
// UIf simplified ternary operator (bool ? val : 0), returns second argument, if the condition (first arg) is true, returns 0 if not
//
// UI.UIf(true,3) // 3
// UI.UIf(false,3) // 0
func UIf(b bool, yes uint64) uint64 {
if b {
return yes
}
return 0
}
// UIfElse ternary operator (bool ? val1 : val2), returns second argument if the condition (first arg) is true, third argument if not
//
// UI.UIfElse(true,3,4) // 3
// UI.UIfElse(false,3,4) // 4
func UIfElse(b bool, yes, no uint64) uint64 {
if b {
return yes
}
return no
}
// UIfZero simplified ternary operator (bool ? val1==0 : val2), returns second argument, if val1 (first arg) is zero, returns val2 if not
//
// UI.UIfZero(0,3) // 3
// UI.UIfZero(4,3) // 4
func UIfZero(val1, val2 uint64) uint64 {
if val1 == 0 {
return val2
}
return val1
}
// UIsZero simplified ternary operator (bool ? val1==0 : val2), returns second argument, if val1 (first arg) is zero, returns val2 if not
//
// I.UIsZero(0,3) // 3
// I.UIsZero(4,3) // 4
func UIsZero(val1, val2 uint) uint {
if val1 == 0 {
return val2
}
return val1
}
// ToS convert int64 to string
//
// I.ToS(int64(1234)) // `1234`
func ToS(num int64) string {
return strconv.FormatInt(num, 10)
}
// ToStr convert int to string
//
// I.ToStr(1234) // `1234`
func ToStr(num int) string {
return strconv.Itoa(num)
}
// UToS convert uint64 to string
//
// I.UToS(uint64(1234)) // `1234`
func UToS(num uint64) string {
return strconv.FormatUint(num, 10)
}
// UToStr convert int to string
//
// I.UToStr(uint(1234)) // `1234`
func UToStr(num uint) string {
return strconv.FormatUint(uint64(num), 10)
}
// Min int64 min of two values
//
// I.Min(int64(3),int64(4)) // 3
func Min(a, b int64) int64 {
if a < b {
return a
}
return b
}
// Max int64 max of two values
//
// I.Max(int64(3),int64(4)) // 4
func Max(a, b int64) int64 {
if a > b {
return a
}
return b
}
// UMin uint64 min of two values
//
// I.UMin(uint64(3),uint64(4)) // 3
func UMin(a, b uint64) uint64 {
if a < b {
return a
}
return b
}
// UMax uint64 max of two values
//
// I.UMax(uint64(3),uint64(4)) // 4
func UMax(a, b uint64) uint64 {
if a > b {
return a
}
return b
}
// MinOf int min of two values
//
// I.MinOf(3,4) // 3
func MinOf(a, b int) int {
if a < b {
return a
}
return b
}
// MaxOf int max of two values
//
// I.MaxOf(3,4) // 4
func MaxOf(a, b int) int {
if a > b {
return a
}
return b
}
// UMinOf uint min of two values
//
// I.MinOf(3,4) // 3
func UMinOf(a, b uint) uint {
if a < b {
return a
}
return b
}
// UMaxOf uint max of two values
//
// I.MaxOf(3,4) // 4
func UMaxOf(a, b uint) uint {
if a > b {
return a
}
return b
}
// ToEnglishNum format ordinal number suffix such as st, nd, rd, and th.
//
// I.ToEnglishNum(241)) // `241st`
// I.ToEnglishNum(242)) // `242nd`
// I.ToEnglishNum(244)) // `244th`
func ToEnglishNum(num int64) string {
if num < 0 {
return ``
}
prefix := ToS(num)
n2 := num % 100
num %= 10
if n2 == 11 || n2 == 12 || n2 == 13 {
prefix += `th`
} else if num == 1 {
prefix += `st`
} else if num == 2 {
prefix += `nd`
} else if num == 3 {
prefix += `rd`
} else {
prefix += `th`
}
return prefix
}
// PadZero converts int64 (first arg) to string with zero padded with maximum length
// I.PadZero(123,5) // `00123`
func PadZero(num int64, length int) string {
str := ToS(num)
slen := len(str)
if slen >= length {
return str
}
return strings.Repeat(`0`, length-slen) + str
}
var romanFig = []int64{100000, 10000, 1000, 100, 10, 1}
var romanI, romanV map[int64]rune
func init() {
// M == ↀ
romanI = map[int64]rune{1: 'I', 10: 'X', 100: 'C', 1000: 'M', 10000: 'ↂ', 100000: 'ↈ'}
romanV = map[int64]rune{1: 'V', 10: 'L', 100: 'D', 1000: 'ↁ', 10000: 'ↇ'}
}
// Roman convert int64 to roman number
//
// I.ToRoman(16)) // output "XVI"
func Roman(num int64) string {
res := []rune{}
x := ' '
for _, z := range romanFig {
digit := num / z
i, v := romanI[z], romanV[z]
switch digit {
case 1:
res = append(res, i)
case 2:
res = append(res, i, i)
case 3:
res = append(res, i, i, i)
case 4:
res = append(res, i, v)
case 5:
res = append(res, v)
case 6:
res = append(res, v, i)
case 7:
res = append(res, v, i, i)
case 8:
res = append(res, v, i, i, i)
case 9:
res = append(res, i, x)
}
num -= digit * z
x = i
}
return string(res)
}