Validating Credit Card Numbers

Sort by

recency

|

483 Discussions

|

  • + 0 comments

    Love this challenge ! It was very fast for me to check the 5 first conditions in regex, but so complex to check the "Not 4 or more consecutive repeated digits" condition.

    For those who come after, I think that a good tip is to create 2 regex patterns. Instead of doing 1 big pattern.

    In my logic, I check all conditions except the first one. Because if already one of them is not valid, I can print "Invalid".

    But if all conditions are valid, then I check the consecutive repeated digit condition.

    For that, I firstly remove "-" if necessary, because it's obviously most complex if not.

    Then I use the regex pattern "([0-9])\1{3,}".

    I didn't know the "back reference" in regex (\1, \2 etc...).

    \1 means : "re-use the last group"

    So ([0-9])\1 means : I capture a digit. And I want this exact same digit after.

    Obviously, by adding {3,} we want that this logic is applied 3 times or more.

    And if this condition is True, it's Invalid, because we don't want to have 4 consecutives repeated digits.

    Note: Be careful to use re.search() and not re.match() for this pattern. Because re.match() only search at the start, whereas re.search() search everywhere in the string.

    Here my code :

    import re
    
    N = int(input())
    
    pattern = r"(^([456])([0-9]{3})-([0-9]{4})-([0-9]{4})-([0-9]{4})$)|(^([456])([0-9]{15})$)"
    # Easier to have a second regex pattern only for the repeated consecutive digit condition
    pattern_repeated_digit = r"([0-9])\1{3,}"
    
    for _ in range(N):
        current_credit_card = input()
    
        if bool(re.match(pattern, current_credit_card)):
            
            # Now that we have check all conditions except repeated digits, we will check this last condition. Before, we have to remove '-'. However, it will be too complex.
            if '-' in current_credit_card:
                current_credit_card = current_credit_card.replace('-', '')
            
            # If there is consecutive repeated digit : Invalid. Else : Valid.
            if bool(re.search(pattern_repeated_digit, current_credit_card)):
                print("Invalid")
            else:
                print("Valid")
        else:
            print("Invalid")
    
  • + 0 comments

    Here is HackerRank Validating Credit Card Numbers in Python solution - https://programmingoneonone.com/hackerrank-validating-credit-card-numbers-solution-in-python.html

  • + 0 comments

    import re regex_format = r'^(?:\d{16}|\d{4}(?:-\d{4}){3})$'

    regex_no_consecutive = r'(\d)(?:-?\1){3}'

    card = input().strip()

    if not re.match(regex_format, card): print("Invalid") else: # Remove hyphens so we can sum digits digits_only = card.replace('-', '') # 2) Check for any digit repeated 4+ times consecutively if re.search(regex_no_consecutive, card): print("Invalid") else: # 3) Sum of digits > 16? total = sum(int(d) for d in digits_only) print("Valid" if total > 16 else "Invalid")

  • + 0 comments
    import re
    
    pattern = r"^[456]\d{3}(-?\d{4}){3}$"
    repeat_pattern = r"(\d)(-?\1){3}"
    for _ in range(int(input())):
        s = input()
        if re.match(pattern, s) and not re.search(repeat_pattern, s):
            print("Valid")
        else:
            print("Invalid")
    
  • + 0 comments
    1. def check(no):
    2. clean = no.replace("-", "")
    3. if len(clean)>16:
    4. return "Invalid"
    5. for i in range(len(clean) - 4):
    6. if clean[i] == clean[i+1] == clean[i+2] == clean[i+3]:
    7. return "Invalid"
    8. match = re.match(r"(([4-6]\d{3})(-?)\d{4}(-?)\d{4}(-?)\d{4})",no)
    9. if match:
    10. return "Valid"
    11. else:
    12. return "Invalid"
      1. for i in range(int(input())):
    13. print(check(input()))