swift luhn algorithm code example
Example: swif luhncheck
func luhnCheck(_ number: String) -> Bool {
var sum = 0
let digitStrings = number.reversed().map { String($0) }
for tuple in digitStrings.enumerated() {
if let digit = Int(tuple.element) {
let odd = tuple.offset % 2 == 1
switch (odd, digit) {
case (true, 9):
sum += 9
case (true, 0...8):
sum += (digit * 2) % 9
default:
sum += digit
}
} else {
return false
}
}
return sum % 10 == 0
}