-
-
Save natecook1000/1eb756d6b10297006137 to your computer and use it in GitHub Desktop.
Luhn check modified with enumerate
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func lunhCheck(number : String) -> Bool | |
{ | |
return reduce(enumerate(reverse(number).map { String($0).toInt()! }), 0) { | |
let odd = $1.0 % 2 == 1 | |
return $0 + (odd ? ($1.1 == 9 ? 9 : ($1.1 * 2) % 9) : $1.1) | |
} % 10 == 0 | |
} | |
lunhCheck("49927398716") | |
lunhCheck("49927398717") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment