HackerRank Python - Validating Credit Card Numbers



You and Fredrick are good friends. Yesterday, Fredrick received credit cards from ABCD Bank. He wants to verify whether his credit card numbers are valid or not. You happen to be great at regex so he is asking for your help!

A valid credit card from ABCD Bank has the following characteristics:



  1. # Enter your code here. Read input from STDIN. Print output to STDOUT
  2. import re
  3. pattern = re.compile(
  4. r'^'
  5. r'(?!.*(\d)(-?\1){3})'
  6. r'[456]\d{3}'
  7. r'(?:-?\d{4}){3}'
  8. r'$')
  9. for _ in range(int(input().strip())):
  10. print('Valid' if pattern.search(input().strip()) else 'Invalid')

download android app