Sunday, 8 March 2015

Hackerrank Valid PAN format Solution

Problem Statement
The equivalent of SSN in India is a PAN number, which is unique to each of its citizens. In any of the country's official documents, the PAN number is listed as follows
<char><char><char><char><char><digit><digit><digit><digit><char>
Your task is to figure out if the PAN number is valid or not. A valid PAN number will have all its letters in uppercase and digits in the same order as listed above.
Source Code:


 #include <cmath>  
 #include <cstdio>  
 #include <vector>  
 #include <iostream>  
 #include <algorithm>  
 using namespace std;  
 int main() {  
   /* Enter your code here. Read input from STDIN. Print output to STDOUT */    
   int t;  
   cin>>t;  
   while(t--){  
     char a[100];  
     int flag = 1;  
     cin>>a;  
     for(int i=0;a[i]!='\0';i++){  
       int temp = a[i];  
       if(i<5){  
         //cout<<a[i]<<" ";  
         //cout<<temp<<" ";  
         if(temp<65 || temp>90){  
           flag = 0;  
         }  
       }else if(i<9){  
         if(temp<48 || temp>57){  
           flag = 0;  
         }  
       }else{  
         if(temp<65 || temp>90){  
           flag = 0;  
         }  
       }  
     }  
     if(flag){  
       cout<<"YES"<<endl;  
     }else{  
       cout<<"NO"<<endl;  
     }  
   }  
   return 0;  
 }  

** The above solution is my own code and it may not be the optimal solution or optimal way to approach the problem but it passes all the testcases in Hackerrank. So if you have any optimal approaches feel free to paste the code as the comment below..... :) :) :)

1 comment:

  1. #include
    #include
    #include
    #include
    #include
    using namespace std;


    int main() {
    int t;
    cin>>t;
    while(t--)
    {
    int i,f=0;
    char s[10];
    cin>>s;
    for(i=0;i<5;i++)
    if(!isupper(s[i]))
    f=1;
    for(i=5;i<9;i++)
    if(!isdigit(s[i]))
    f=1;
    if(!isupper(s[9]))
    f=1;

    if(f)
    cout<<"NO\n";
    else
    cout<<"YES\n";

    }
    return 0;
    }

    ReplyDelete