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')
Please click on the like button if it worked

Solution not working or have any suggestions? Please send an email to [email protected]


donate a cup of tea :)


Join Our Facebook Group

Share this solution






codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube