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.
Enter your code here. Read input from STDIN. Print output to STDOUT
import re
N = int(input())
credit_num = []
for _ in range(N):
cred = input().strip() # Read each line and strip any extra whitespace
credit_num.append(cred) # Append the entire line as a single entry
for n in credit_num:
if (re.match(r'^[4-6]', n) and
len(re.sub(r'[^0-9]', '', n)) == 16 and
re.match(r'^[0-9-]+', n) or re.match(r'^\d{16}$', n)) and
not re.search(r'(\d)\1{3,}', re.sub(r'[^0-9]', '', n))):
print("Valid")
else:
print("Invalid")
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Validating Credit Card Numbers
You are viewing a single comment's thread. Return to all comments →
Enter your code here. Read input from STDIN. Print output to STDOUT
import re
N = int(input()) credit_num = [] for _ in range(N): cred = input().strip() # Read each line and strip any extra whitespace credit_num.append(cred) # Append the entire line as a single entry
for n in credit_num: if (re.match(r'^[4-6]', n) and len(re.sub(r'[^0-9]', '', n)) == 16 and re.match(r'^[0-9-]+', n) or re.match(r'^\d{16}$', n)) and not re.search(r'(\d)\1{3,}', re.sub(r'[^0-9]', '', n))): print("Valid") else: print("Invalid")