jmpark0118 / CODING_TEST_PRACTICE

0 stars 0 forks source link

Practice>Python>Regex and Parsing>Validating Credit Card Numbers #102

Open jmpark0118 opened 3 years ago

jmpark0118 commented 3 years ago

image image image

출처 : https://www.hackerrank.com/challenges/validating-credit-card-number/problem

jmpark0118 commented 3 years ago

import re

if __name__ == '__main__':
    for _ in range(int(input())):
        card = input()
        rule1 = re.match(r'(\d){4}(-){0,1}(\d){4}(-){0,1}(\d){4}(-){0,1}(\d){4}$', card)
        if rule1 is None:
            print("Invalid")
        else:
            card = card.replace('-','')
            if int(card[0]) not in [4,5,6]:
                print("Invalid")
            elif re.search(r'(\d)\1{3}', card) is not None:
                print("Invalid")
            else:
                print("Valid")