Validating Credit Card Numbers

  • + 0 comments
    # Enter your code here. Read input from STDIN. Print output to STDOUT
    import re
    import itertools as it
    
    N = int(input())
    pattern = r'^[4-6][0-9]{3}([0-9]{12}|(-[0-9]{4}){3})$'
    for _ in range(N):
        number = str(input())
        rawnumber = number.replace('-', '')
        consecutive = any(x>=4 for x in [len(list(i)) for k, i in it.groupby(rawnumber)])
        if re.match(pattern, number) and not consecutive:
            print("Valid")
        else:
            print("Invalid")