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
def check_no_of_digits(p):
digits_list = [i for i in p if i.isdigit()]
if len(digits_list) == 16:
return True
return False
def check_starting_digits(q):
if q[0]=='4' or q[0]=='5' or q[0]=='6':
return True
return False
def check_digits(r):
for i in r:
if not i.isdigit():
return False
return True
def check_hyphens(s):
if "-" not in s:
return True
else:
l = s.split("-")
for i in l:
if len(i)!=4:
return False
return True
def consecutive_check(t):
lst = [i for i in t if i.isdigit()]
#print(lst)
for i in range(len(lst) - 3): # Ensure we check till len(lst) - 4
if lst[i] == lst[i+1] == lst[i+2] == lst[i+3]:
return True
return False
n = int(input())
c = 0
for i in range(n):
x = input()
y = check_no_of_digits(x)
z = check_starting_digits(x)
a = check_digits(x)
b = check_hyphens(x)
c = consecutive_check(x)
if y and z and a and not c and b:
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
def check_no_of_digits(p): digits_list = [i for i in p if i.isdigit()] if len(digits_list) == 16: return True return False def check_starting_digits(q): if q[0]=='4' or q[0]=='5' or q[0]=='6': return True return False def check_digits(r): for i in r: if not i.isdigit(): return False return True def check_hyphens(s): if "-" not in s: return True else: l = s.split("-") for i in l: if len(i)!=4: return False return True def consecutive_check(t): lst = [i for i in t if i.isdigit()] #print(lst) for i in range(len(lst) - 3): # Ensure we check till len(lst) - 4 if lst[i] == lst[i+1] == lst[i+2] == lst[i+3]: return True return False n = int(input()) c = 0 for i in range(n): x = input() y = check_no_of_digits(x) z = check_starting_digits(x) a = check_digits(x) b = check_hyphens(x) c = consecutive_check(x) if y and z and a and not c and b: print("Valid") else: print("Invalid")