-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
68 lines (55 loc) · 1.1 KB
/
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package bankverification
import (
"strconv"
"strings"
)
const (
AlgorithmMod10 = "mod10"
AlgorithmMod11 = "mod11"
)
func padLeft(s string, length int, padChar rune) string {
if len(s) > length {
return s
}
padding := strings.Repeat(string(padChar), length-len(s))
return padding + s
}
func mod10(value string) bool {
runes := []rune(value)
sum := 0
for i := len(runes) - 1; i >= 0; i-- {
digit, _ := strconv.Atoi(string(runes[i]))
weight := 1
if (len(runes)-i)%2 == 0 {
weight = 2
}
tmp := digit * weight
if tmp > 9 {
tmp -= 9
}
sum += tmp
}
return sum%10 == 0
}
func mod11(value string) bool {
if len(value) > 11 {
return false
}
weights := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1}
runes := []rune(value)
sum := 0
for i := len(runes) - 1; i >= 0; i-- {
digit, _ := strconv.Atoi(string(runes[i]))
sum += digit * weights[len(runes)-1-i]
}
return sum%11 == 0
}
func filter(slice []string, predicate func(string) bool) []string {
var result []string
for _, value := range slice {
if predicate(value) {
result = append(result, value)
}
}
return result
}