Last active
June 30, 2018 13:29
-
-
Save amosavian/82319c0c2186fd7f4417deaa7ccd4edd to your computer and use it in GitHub Desktop.
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 validate(id: String) -> Bool { | |
guard id.characters.count < 11, let num = Int64(id) else { return false } | |
guard (num / 10) % 1000000 > 0 else { return false } | |
let checkNumber = Int(num % 10) | |
let digits = id.characters.map { Int(String($0))! } | |
let digitCount = digits.count | |
let sum = digits.dropLast().enumerated().reduce(0) { $0 + (digitCount - $1.offset) * $1.element } | |
let r = sum % 11 | |
return (r < 2 && checkNumber == r) || (r >= 2 && checkNumber == (11 - r)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment