Skip to content

Instantly share code, notes, and snippets.

@AnthonyBY
Last active November 4, 2017 16:28
Show Gist options
  • Save AnthonyBY/24bdafe49edf1f37672185fd1a48321b to your computer and use it in GitHub Desktop.
Save AnthonyBY/24bdafe49edf1f37672185fd1a48321b to your computer and use it in GitHub Desktop.
Valid PAN format. Swift 3. HackerRank
import Foundation
// read the integer n
let n = Int(readLine()!)!
var arr = Array(repeating: "", count: n)
for index in 0...n-1 {
arr[index] = String(readLine()!)
}
func isPANValid(code : String) -> Bool {
let regex = try! NSRegularExpression(pattern: "^[A-Z]{5}[0-9]{4}[A-Z]{1}$")
return regex.numberOfMatches(in: code, range: NSRange(location:0, length: code.characters.count)) == 1
}
for string in arr {
if isPANValid(code: string) {
print("YES")
} else {
print("NO")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment