0 < N < 100
Find if credit card number is valid or not
Carvia Tech | September 02, 2019 | 2 min read | 442 views | Python Coding Problems
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:
► It must start with a , or .
► It must contain exactly digits.
► It must only consist of digits (-).
► It may have digits in groups of , separated by one hyphen "-".
► It must NOT use any other separator like ' ' , '_', etc.
► It must NOT have or more consecutive repeated digits.
This is in reference to Python 3
The first line of input contains an integer N. The next N lines contain credit card numbers.
Print 'Valid' if the credit card number is valid. Otherwise, print 'Invalid'. Do not print the quotes.
6 4123456789123456 5123-4567-8912-3456 61234-567-8912-3456 4123356789123456 5133-3367-8912-3456 5123 - 3567 - 8912 - 3456
Valid Valid Invalid Valid Invalid Invalid
This answer passes all the tests
We are going to keep logic in main function only such that we are able to build array as we will be getting data from input.
You shall create a
script.py file and paste the below code in it.
n = int(input()) This takes input from command line. Please note that input type is going to be in string
format and we have to use
int() to convert from
for i in range(n): arr = input() Now, we are staying in the range so that program stays up till we want to test numbers only and collecting input of prospective credit card numbers in
check_validity(arr) here we are calling function
check_validity for check if the number given is valid or not.
from itertools import groupby def check_validity(num): if num.startswith('4') or num.startswith('5') or num.startswith('6'): if '-' in num: if len(num)==19: num = ''.join(i for i in num.split('-') if len(i) == 4) if len(num)==16: a = list(num) b = [str(i) for i in list(range(10))] if set(a).issubset(b): groups = groupby(num) result = [sum(1 for _ in group) for label, group in groups] if any(y >= 4 for y in result): print('Invalid') else: print('Valid') else: print('Invalid') else: print('Invalid') else: print('Invalid') elif len(num)==16: a = list(num) b = [str(i) for i in list(range(10))] if set(a).issubset(b): groups = groupby(num) result = [sum(1 for _ in group) for label, group in groups] if any(y >= 4 for y in result): print('Invalid') else: print('Valid') else: print('Invalid') else: print('Invalid') else: print('Invalid') if __name__ == '__main__': n = int(input()) for i in range(n): arr = input() check_validity(arr)
Top articles in this category:
- Python coding challenges for interviews
- Top 100 interview questions on Data Science & Machine Learning
- Google Data Scientist interview questions with answers
- Flask Interview Questions
- Write a program to find if a number is Lychrel Number in Python
- Difference between Loss, Accuracy, Validation loss, Validation accuracy in Keras
- Spaced Out?
Find more on this topic:
Data science, machine learning, python, R, big data, spark, the Jupyter notebook, and much more
Last updated 1 week ago
Recommended books for interview preparation:
- Configure Logging in gunicorn based application in docker container
- Connect to Cassandra with Python 3.x and get Pandas Dataframe
- Connect to MySQL with Python 3.x and get Pandas Dataframe
- Connect to Postgresql with Python 3.x and get Pandas Dataframe
- Python - Get Google Analytics Data
- Installing PySpark with Jupyter notebook on Ubuntu 18.04 LTS
- Python send GMAIL with attachment
- Send rich text multimedia email in Python
- Blueprints in Flask API Development
- Singleton Design Pattern in Python