Find if credit card number is valid or not

Upasana | September 02, 2019 | 2 min read | 500 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.

Note

This is in reference to Python 3

Input Format

The first line of input contains an integer N. The next N lines contain credit card numbers.

Constraints

0 < N < 100

Output Format

Print 'Valid' if the credit card number is valid. Otherwise, print 'Invalid'. Do not print the quotes.

Sample Input

6
4123456789123456
5123-4567-8912-3456
61234-567-8912-3456
4123356789123456
5133-3367-8912-3456
5123 - 3567 - 8912 - 3456

Sample Output

Valid
Valid
Invalid
Valid
Invalid
Invalid

Solution

Note

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.

1st line: 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 string to int

2nd line: 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 arr.

3rd line: check_validity(arr) here we are calling function check_validity for check if the number given is valid or not.

Finding valid credit card number: python
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:
  1. Python coding challenges for interviews
  2. Write a program to find if a number is Lychrel Number in Python
  3. Find perfect abundant or deficient factors in python
  4. Difference between Loss, Accuracy, Validation loss, Validation accuracy in Keras
  5. Write a program to check if the given word is Isogram & Pair isogram in python
  6. Explain a probability distribution that is not normal and how to apply that
  7. Top 100 interview questions on Data Science & Machine Learning

Recommended books for interview preparation:

Find more on this topic: