Validating Credit Card Numbers

  • + 0 comments

    Test case 3 is failed and i am not getting the problem where is : This is my code:

    import re

    def check(s): #1check if no of digits are 16 or not after removing - if len(s.replace("-",""))==16: for i in s: if i=="-" or i.isdigit(): continue else: return "Invalid"

        pattern=r"[456]\d{3}(-?\d{4}){3}"
        s_new=s.replace('-',"")
        c=1
    
        #checking repetetion of same digit equal to or more than 4
        for i in range(len(s_new)-1):
            if "-" in s_new:
                match=re.finditer(r"-",s_new)
                index=[x.start() for x in match]
                if index==[3,7,11]:
                    pass
                else:
                    return "Invalid"
    
            if s_new[i]==s_new[i+1]:
                c+=1
                if c==4:
                    return "Invalid"
                continue
            else:
                c=1
        if c==1:
            if re.match(pattern,s):
                return "Valid"
            else:
                return "Invalid"
    
    else:
        return "Invalid"
    

    n=int(input()) for _ in range(n): s=input() result=check(s) print(result)