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.
importredefis_valid_credit_card(number):# check if it starts with 4, 5 or 6ifnotre.match(r'^[4-6]',number):returnFalse# check if it contains exactly 16 digitsifnotre.match(r'^\d{16}$|^(\d{4}-){3}\d{4}$',number):returnFalse# check if it only contains digitsifnotre.match(r'^\d+$',number.replace("-","")):returnFalse# check if it does not have 4 or more consecutive repeated digitsifre.search(r'(\d)\1{3,}',number.replace("-","")):returnFalsereturnTruen=int(input())credit_cards=[]foriinrange(n):credit_cards.append(input())foriinrange(n):ifis_valid_credit_card(credit_cards[i]):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 →