-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtckimlikno.go
64 lines (50 loc) · 881 Bytes
/
tckimlikno.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
package tckimlikno
import (
"strconv"
)
// IsValid returns whether it is a valid number
func IsValid(tc string) bool {
if tc[0] == '0' {
return false
}
if len(tc) != 11 {
return false
}
if isDigit(tc) != true {
return false
}
arr := make([]int, 11)
for i, j := range tc {
arr[i], _ = strconv.Atoi(string(j))
}
var oddSum = arr[0]
var evenSum int
for i := 1; i < 8; i++ {
evenSum += arr[i] // arr[1] 3 5 7
i++
oddSum += arr[i] // arr[2] 4 6 8
}
n := ((oddSum * 7) - evenSum) % 10
if n < 0 {
n += 10
}
if n != arr[9] {
return false
}
m := (oddSum + evenSum + arr[9]) % 10
if m != arr[10] {
return false
}
return true
}
// isDigit returns whether the given string consists of numbers only
func isDigit(s string) bool {
b := true
for _, c := range s {
if c < '0' || c > '9' {
b = false
break
}
}
return b
}