Problem Statement
A description of the problem can be found on Hackerrank.
Solution
Regex:
[A-Z]{5}\d{4}[A-Z]
I created solution in:
All solutions are also available on my GitHub.
Ruby
1 2 3 4 5 6 7 8 9 |
tests = gets.chomp.to_i tests.times do input = gets.chomp.to_s if input =~ /[A-Z]{5}\d{4}[A-Z]/ puts "YES" else puts "NO" end end |
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.util.*; public class ValidPanFormat { private static final String PAN = "[A-Z]{5}\\d{4}[A-Z]"; public static void main(String[] args) { Scanner stdin = new Scanner(System.in); int tests = Integer.parseInt(stdin.nextLine()); for(int i = 0; i < tests; i++) { System.out.println(isPan(stdin.nextLine())); } stdin.close(); } private static String isPan(String s) { if(s.matches(PAN)) { return "YES"; } else { return "NO"; } } } |
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
'use strict'; const PAN = /[A-Z]{5}\d{4}[A-Z]/; const processData = input => { let lines = input.split('\n').slice(1); let isPan = s => { if(s.match(PAN)) { return "YES"; } else { return "NO"; } }; console.log(lines.map(isPan).join('\n')); }; process.stdin.resume(); process.stdin.setEncoding("ascii"); var _input = ""; process.stdin.on("data", input => _input += input); process.stdin.on("end", () => processData(_input)); |
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import scala.io.Source object ValidPanFormat extends App { private[this] val PAN = "[A-Z]{5}\\d{4}[A-Z]" val lines = Source.stdin.getLines().drop(1) val pans = lines.map(isPan) println(pans.mkString("\n")) def isPan(s: String): String = { if(s.matches(PAN)) { "YES" } else { "NO" } } } |