Validating Credit Card Numbers Hacker Rank Solution – Queslers

Problem : Validating Credit Card Numbers Hacker Rank Solution

 You and Fredrick are good friends. Yesterday, Fredrick received N 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 4, 5 or 6.
► It must contain exactly 16 digits.
► It must only consist of digits (0-9).
► It may have digits in groups of 4, separated by one hyphen “-“.
► It must NOT use any other separator like ‘ ‘ , ‘_’, etc.
► It must NOT have 4 or more consecutive repeated digits. 

Example :

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 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

Explanation :

4123456789123456 : Valid
5123-4567-8912-3456 : Valid
61234-576-8912-3456 : Invalid, because the card number is not divided into equal groups of 4.
4123356789123456 : Valid
5133-3367-8912-3456 : Invalid, consecutive digits 3333 is repeating 4 times.
5123 – 4567 – 8912 – 3456 : Invalid, because space ‘  ‘ and - are used as separators.

Validating Credit Card Numbers Hacker Rank Solution in python 2

import re
ccn = re.compile(r'[4-6][0-9]{3}[-]?[0-9]{4}[-]?[0-9]{4}[-]?[0-9]{4}')

def check_consecutive(s):
    n = len(s)
    ch = s[0]
    count = 1
    for i in xrange(1, n):
        if s[i] == '-':
            continue
        if s[i] == ch:
            count += 1
            if count == 4:
                return False
        else:
            ch = s[i]
            count = 1
    return True

def check_hyphen(s):
    if s.count('-') == 0 or s.count('-') == 3:
        return True
    else:
        return False

N = input()
for _ in xrange(N):
    s = raw_input().strip()
    if ccn.match(s) and check_consecutive(s) and check_hyphen(s):
        print 'Valid'
    else:
        print 'Invalid'

Validating Credit Card Numbers Hacker Rank Solution in python 3

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

Validating Credit Card Numbers Hacker Rank Solution in python 3

# Enter your code here. Read input from STDIN. Print output to STDOUT
import re
n = int(raw_input())
for i in range(n):
    s=raw_input().strip()
    if len(s) != 16 and len(s) != 19:
        print "Invalid"
    elif s[0] not in ['4','5','6']:
        print "Invalid"
    elif len(s) == 16:
        print re.match(r'\d{16,16}',s) and not re.search(r'(\d)\1\1\1',s) and "Valid" or "Invalid"
    else:
        print re.match(r'\d\d\d\d-\d\d\d\d-\d\d\d\d-\d\d\d\d',s) and not re.search(r'(\d)\1\1\1',s.replace('-','')) and "Valid" or "Invalid"

Validating Credit Card Numbers Hacker Rank Solution in pypy

import re

start_with_456 = lambda x: x[0]=='4' or x[0]=='5' or x[0]=='6'

contain_16_digits = lambda x: bool(re.fullmatch(r'\d{16}', ''.join(x.split('-'))))

groups_of_4 = lambda x: all([len(i)==4 for i in x.split('-') if '-' in x])
    
repeating_characters = lambda x: not(bool(re.search(r'(\d)\1{3,}', ''.join(x.split('-')))))
    
tests = [start_with_456, contain_16_digits, groups_of_4, repeating_characters]

N = int(input())
for _ in range(N):
    s = input()
    if all(map(lambda x: x(s), tests)):
        print('Valid')
    else:
        print('Invalid')
Validating Credit Card Numbers Hacker Rank Solution Review:

In our experience, we suggest you solve this Validating Credit Card Numbers Hacker Rank Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

Validating Credit Card Numbers is available on Hacker Rank for Free, if you are stuck anywhere between compilation, just visit Queslers to get all Hacker Rank Solution

Validating Credit Card Numbers Hacker Rank Solution Conclusion:

I hope this Validating Credit Card Numbers Hacker Rank Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Hacker Rank, Leetcode, Codechef, Codeforce Solution.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Hacker Rank Problem & Solutions >>

Mini-Max Sum Hacker Rank Solution

String Validators Hacker Rank Solution

Text Alignment Hacker Rank solution

String validators Hacker Rank Solution

Staircase Hacker Rank Solution

Leave a Reply

Your email address will not be published.