Last active
October 22, 2018 20:48
-
-
Save hsharghi/5920617a90568b58af7ba584b3e884b5 to your computer and use it in GitHub Desktop.
Validate Iranian national code in Swift 4 - کنترل صحت کد ملی ایران
This file contains 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(input: String) -> Bool { | |
let digits = input.map { Int(String($0)) } | |
guard digits.count == 10 && digits.count == input.count else { | |
return false | |
} | |
let check = digits[9]! | |
let remainder = digits.prefix(upTo: 9).enumerated().reduce(0) { | |
$0 + $1.element! * (10 - $1.offset) | |
} % 11 | |
return remainder < 2 ? check == remainder : check + remainder == 11 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment