Technology

Programming

Post Page Advertisement [Top]

competitive programmingHaker Rank SolutionsPython

Validating Credit Card Numbers - Hacker Rank Problem Solution Python Code

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.
Examples:
Valid Credit Card Numbers
4253625879615786
4424424424442444
5122-2368-7954-3214
Invalid Credit Card Numbers
42536258796157867       #17 digits in card number → Invalid 
4424444424442444        #Consecutive digits are repeating 4 or more times → Invalid
5122-2368-7954 - 3214   #Separators other than '-' are used → Invalid
44244x4424442444        #Contains non digit characters → Invalid
0525362587961578        #Doesn't start with 4, 5 or 6 → Invalid
Input Format
The first line of input contains an integer .
The next  lines contain credit card numbers.
Constraints
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
Explanation
4123456789123456 : Valid
5123-4567-8912-3456 : Valid
61234--8912-3456 : Invalid, because the card number is not divided into equal groups of .
4123356789123456 : Valid
51-67-8912-3456 : Invalid, consecutive digits  is repeating  times.
5123456789123456 : Invalid, because space '  ' and - are used as separators.


# Enter your code here. Read input from STDIN. Print output to STDOUTdef AllDigits(credit_card):
    for digit in credit_card:
        if '0' <= digit <= '9':
            pass        else:
            return False    return True

def ConsecutiveRepeatedDigits(credit_card):
    count = 1    for i in range(15):
        if credit_card[i] == credit_card[i + 1]:
            count += 1            if count == 4:
                return True        else:
            count = 1    return False

def DigitsAndHyphenOnly(credit_card):
    for digit in credit_card:
        if digit == '-' or ('0' <= digit <= '9'):
            pass        else:
            return False    return True

def DigitsSepratedCorrectly(credit_card):
    for i in range(4, len(credit_card), 5):
        if credit_card[i] != '-':
            return False    return True

for i in range(int(input())):
    credit_card = input()
    if AllDigits(credit_card) == True:
        if len(credit_card) != 16:
            print("Invalid")
        elif not ('4' <= credit_card[0] <= '6'):
            print("Invalid")
        elif ConsecutiveRepeatedDigits(credit_card) == True:
            print("Invalid")
        else:
            print("Valid")
    elif DigitsAndHyphenOnly(credit_card) == True:
        if DigitsSepratedCorrectly(credit_card) == False:
            print("Invalid")
        else:
            credit_card = credit_card.split('-')
            credit_card = ''.join(credit_card)
            if len(credit_card) != 16:
                print("Invalid")
            elif not ('4' <= credit_card[0] <= '6'):
                print("Invalid")
            elif ConsecutiveRepeatedDigits(credit_card) == True:
                print("Invalid")
            else:
                print("Valid")
    else:
        print("Invalid")



No comments:

Post a Comment

Bottom Ad [Post Page]

| Designed by Colorlib