Skip to content

Instantly share code, notes, and snippets.

@hsharghi
Last active October 22, 2018 20:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hsharghi/5920617a90568b58af7ba584b3e884b5 to your computer and use it in GitHub Desktop.
Save hsharghi/5920617a90568b58af7ba584b3e884b5 to your computer and use it in GitHub Desktop.
Validate Iranian national code in Swift 4 - کنترل صحت کد ملی ایران
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