Skip to content

Instantly share code, notes, and snippets.

@jonahaung
Created November 16, 2019 16:01
Show Gist options
  • Save jonahaung/bff96684bbef488e66b6787b6480fab2 to your computer and use it in GitHub Desktop.
Save jonahaung/bff96684bbef488e66b6787b6480fab2 to your computer and use it in GitHub Desktop.
Singapore NRIC Checker
func isSingaporeNRIC(fin :String) -> Bool {
guard fin.count == 9 else { return false }
let str = fin.uppercased()
let characters = [Character](str)
var weight = 0
weight += Int(String(characters[1]))! * 2
weight += Int(String(characters[2]))! * 7
weight += Int(String(characters[3]))! * 6
weight += Int(String(characters[4]))! * 5
weight += Int(String(characters[5]))! * 4
weight += Int(String(characters[6]))! * 3
weight += Int(String(characters[7]))! * 2
if characters[0] == "T" || characters[0] == "G"{
weight += 4
}
let temp = weight % 11;
let st = ["J","Z","I","H","G","F","E","D","C","B","A"]
let fg = ["X","W","U","T","R","Q","P","N","M","L","K"]
var theAlpha : String!
if characters[0] == "S" || characters[0] == "T" {
theAlpha = st[temp]
}
else if characters[0] == "F" || characters[0] == "G" {
theAlpha = fg[temp]
}
if String(characters[8]) == theAlpha {
return true
}
return false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment