Validating Credit Card Numbers

Sort by

recency

|

482 Discussions

|

  • + 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()))
  • + 0 comments

    Here is my solution, wasted too much time to understand that i was not tracking "--", but fixed and it worked. ` import re

    n_cards = int(input()) if n_cards == 0: print()

    cards=[] for i in range(n_cards): cards.append(input().strip())

    pattern1 = r"^[456][0-9-]*$" # card format only digits or - pattern2 = r"(\d)\1{3,}" # 4 or more consecutive digits for card in cards: if len(card.replace("-","")) != 16: print("Invalid")
    else: # length is 16 without hyphens if bool(re.search(pattern1,card)) and not bool(re.search(pattern2,card.replace("-",""))): # if conditions match if "--" in card: print("Invalid") elif "-" in card: card_splits = any(a>4 for a in list(map(len,card.split("-")))) if card_splits:# if hyphen then groups of 4 numbers print("Invalid") # no invalid else:# yes valid print("Valid") else: print("Valid") else: print("Invalid")