-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Add credit card string validator #5583
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
cclauss
merged 17 commits into
TheAlgorithms:master
from
Bhargavishnu:add_cc_validator_string
Oct 26, 2021
Merged
Changes from 13 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
c8c07a4
Add credit card validator
Bhargavishnu ed940d5
d71d1c4
Add return type hint
Bhargavishnu 72c4d54
Add test cases for validator function
Bhargavishnu 5cc789c
Add test cases
Bhargavishnu 0ac42d7
Feature: Rename file
Bhargavishnu 007ff6c
Update strings/cc_validator.py
Bhargavishnu ef666cb
Merge branch 'add_cc_validator_string' of https://github.com/Bhargavi…
Bhargavishnu 2832a33
Update strings/cc_validator.py
Bhargavishnu c8b141a
Update strings/cc_validator.py
Bhargavishnu 370eed0
Merge branch 'add_cc_validator_string' of https://github.com/Bhargavi…
Bhargavishnu b81054c
Review: Fix redundant checks
Bhargavishnu 981282b
Review: Refactor
Bhargavishnu a09dca4
Fix: Update test cases
Bhargavishnu ac83913
Refactor
Bhargavishnu 1524110
Merge branch 'master' into add_cc_validator_string
Bhargavishnu 6985f7b
Update credit_card_validator.py
cclauss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
def validate_initial_digits(credit_card_number: str) -> bool: | ||
""" | ||
Function to validate initial digits of a given credit card number. | ||
>>> valid = "4111111111111111 41111111111111 34 35 37 412345 523456 634567" | ||
>>> all(validate_initial_digits(cc) for cc in valid.split()) | ||
True | ||
>>> invalid = "32323 36111111111111" | ||
>>> all(validate_initial_digits(cc) is False for cc in invalid.split()) | ||
True | ||
""" | ||
first_digit = int(credit_card_number[0]) | ||
if first_digit in (4, 5, 6): | ||
return True | ||
elif first_digit == 3: | ||
second_digit = int(credit_card_number[1]) | ||
return second_digit in (4, 5, 7) | ||
|
||
return False | ||
|
||
|
||
def luhn_validation(credit_card_number: str) -> bool: | ||
""" | ||
Function to luhn algorithm validation for a given credit card number. | ||
>>> luhn_validation('4111111111111111') | ||
True | ||
>>> luhn_validation('36111111111111') | ||
True | ||
>>> luhn_validation('41111111111111') | ||
False | ||
""" | ||
cc_number = credit_card_number | ||
total = 0 | ||
half_len = len(cc_number) - 2 | ||
for i in range(half_len, -1, -2): | ||
# double the value of every second digit | ||
digit = int(cc_number[i]) | ||
digit *= 2 | ||
# If doubling of a number results in a two digit number | ||
# i.e greater than 9(e.g., 6 × 2 = 12), | ||
# then add the digits of the product (e.g., 12: 1 + 2 = 3, 15: 1 + 5 = 6), | ||
# to get a single digit number. | ||
if digit > 9: | ||
digit %= 10 | ||
digit += 1 | ||
cc_number = cc_number[:i] + str(digit) + cc_number[i + 1 :] | ||
total += digit | ||
|
||
# Sum up the remaining digits | ||
for i in range(len(cc_number) - 1, -1, -2): | ||
total += int(cc_number[i]) | ||
|
||
return total % 10 == 0 | ||
|
||
|
||
def validate_credit_card_number(number: str) -> bool: | ||
""" | ||
Function to validate the given credit card number. | ||
>>> validate_credit_card_number('4111111111111111') | ||
Given number(4111111111111111) is Valid | ||
>>> validate_credit_card_number('helloworld$') | ||
Invalid number(helloworld$) given: Contains alphabets or special characters | ||
>>> validate_credit_card_number('32323') | ||
Invalid number(32323) given: Check number length | ||
>>> validate_credit_card_number('32323323233232332323') | ||
Invalid number(32323323233232332323) given: Check number length | ||
>>> validate_credit_card_number('36111111111111') | ||
Invalid number(36111111111111) given: Check starting number | ||
>>> validate_credit_card_number('41111111111111') | ||
Invalid number(41111111111111) given: Invalid Number | ||
""" | ||
error_message = f"Invalid number({number}) given: " | ||
credit_card_number = str(number) | ||
if not credit_card_number.isdigit(): | ||
print(error_message + "Contains alphabets or special characters") | ||
return False | ||
|
||
credit_card_number_length = len(credit_card_number) | ||
if credit_card_number_length < 13 and credit_card_number_length > 16: | ||
print(error_message + "Check number length") | ||
return False | ||
|
||
if not validate_initial_digits(credit_card_number): | ||
print(error_message + "Check starting number") | ||
return False | ||
|
||
if not luhn_validation(credit_card_number): | ||
print(error_message + "Invalid Number") | ||
return False | ||
|
||
print(f"Given number({number}) is Valid") | ||
return True | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No number can be < 13 and > 16!!!
Python allows...
You can use this code to show yourself how it works:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
JS programmers are outdated ! 😂