We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Validating Credit Card Numbers
Validating Credit Card Numbers
Sort by
recency
|
479 Discussions
|
Please Login in order to post a comment
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")
n=int(input()) a=[] for i in range(n): b=input() a.append(b) c=0 for i in a: if i[0] not in '456': c=c+1 if len(i)==16: for q in i: if q not in "0,1,2,3,4,5,6,7,8,9": c=c+1 if len(i)==19: for w in range(len(i)): if w in [4,8,12]: i[w]!="-" c=c+1 else: i[w]!="0,1,2,3,4,5,6,7,8,9" c=c+1 for k in range(0,len(i)-4): x=i[k] z=0 for l in range(k+1,k+5): if i[l]==x: z=z+1 if z>=3: c=c+1
if c==0: print("Valid") else: print("Invalid")