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.
defvalidateCreditCardNumber(card_number):digit_counter=0group_counter=0last_digit=""last_digit_counter=1# It must start with a 4, 5 or 6.first_digit=int(card_number[0])iffirst_digit!=4andfirst_digit!=5andfirst_digit!=6:return"Invalid"forxincard_number:# It must only consist of digits (0-9).ifx.isdigit():digit_counter+=1group_counter+=1# It must contain exactly 16 digits.ifdigit_counter>16:return"Invalid"# It must NOT have 4 or more consecutive repeated digits.iflast_digit==x:last_digit_counter+=1eliflast_digit_counter>=4:return"Invalid"else:last_digit=xlast_digit_counter=1# It must NOT use any other separator like ' ' , '_', etc.elifx!="-":return"Invalid"# It may have digits in groups of 4, separated by one hyphen "-".elifgroup_counter==4:group_counter=0else:return"Invalid"ifdigit_counter!=16:return"Invalid"else:return"Valid"defmain():n=int(input())cards_numbers=[]for_inrange(n):card_number=input()print(validateCreditCardNumber(card_number))main()
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 →