In this HackerRank Validating Credit Card Numbers problem solution, You and Fredrick are good friends. Yesterday, Fredrick received N credit cards from ABCD Bank. He wants to verify whether his credit card numbers are valid or not. You happen to be great at regex so he is asking for your help!

HackerRank Validating Credit Card Numbers in python problem solution

HackerRank Validating Credit Card Numbers in python problem solution

from itertools import groupby

def Validate(string):
    if string[0] not in '456':
        return 'Invalid'
    if len([1 for i in string if i.isdigit()]) != 16:
        return 'Invalid'
    if len([1 for i in string if i not in '0123456789-']) > 0:
        return 'Invalid'
    if '-' in string and len([1 for i in map(len, string.split('-')) if i<>4]) > 0:
        return 'Invalid'
    if len([1 for i in [sum(1 for i in g) for k,g in groupby(string.replace("-", ""))] if i>=4]) > 0:
        return 'Invalid'
    return 'Valid'

for _ in xrange(input()):
    print Validate(raw_input())