-
Notifications
You must be signed in to change notification settings - Fork 0
/
numbers.go
58 lines (51 loc) · 1.15 KB
/
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
package utils
import (
"fmt"
"strconv"
)
// MustAtoi converts a string to a number, but panics if the conversion fails.
// This avoids error handling, but must be used when the calling code is sure the string represents a number.
func MustAtoi(strNum string) int {
num, err := strconv.Atoi(strNum)
if err != nil {
panic(fmt.Sprintf("Atoi in MustAtoi failed trying to convert the string '%s' to a number", strNum))
}
return num
}
// Abs returns the absolute value of an int
func Abs(number int) int {
if number < 0 {
return -number
}
return number
}
// Max returns the max between two numbers
func Max(a, b int) int {
if a > b {
return a
}
return b
}
// Min returns the min between two numbers
func Min(a, b int) int {
if b < a {
return b
}
return a
}
// MinMax returns the min and max between two numbers
func MinMax(a, b int) (int, int) {
if b < a {
return b, a
}
return a, b
}
// StringDigits returns a slice of ints with the digits in a string. The argument must be a string only with runes
// that represent digits
func StringDigits(str string) []int {
var res []int
for _, c := range str {
res = append(res, int(c-'0'))
}
return res
}