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.
importsysimportredefis_valid_format(card_number):"""Check if the card number has the correct format."""format_pattern=r"^[456]\d{3}([-]?\d{4}){3}$"returnbool(re.match(format_pattern,card_number))defhas_repeated_digits(card_number):"""Check if the card number contains four consecutive repeated digits."""cleaned_number=''.join(card_number.split('-'))returnbool(re.search(r"(.)\1{3}", cleaned_number))defvalidate_card_numbers(card_numbers):"""Validate a list of card numbers and return the results."""results=[]forcardincard_numbers:ifis_valid_format(card)andnothas_repeated_digits(card):results.append("Valid")else:results.append("Invalid")returnresultsif__name__=="__main__":text=sys.stdin.read().splitlines()[1:]results=validate_card_numbers(text)print("\n".join(results))
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 →