-
-
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
Changes from 2 commits
c8c07a4
ed940d5
d71d1c4
72c4d54
5cc789c
0ac42d7
007ff6c
ef666cb
2832a33
c8b141a
370eed0
b81054c
981282b
a09dca4
ac83913
1524110
6985f7b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,68 @@ | ||||||
def validate_initial_digits(credit_card_number: str) -> bool: | ||||||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please rename this file to have a more self-documenting name like |
||||||
first_digit = int(credit_card_number[0]) | ||||||
if first_digit == 4 or first_digit == 5 or first_digit == 6: | ||||||
Bhargavishnu marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
return True | ||||||
elif first_digit == 3: | ||||||
second_digit = int(credit_card_number[1]) | ||||||
if second_digit == 7 or second_digit == 4 or second_digit == 5: | ||||||
Bhargavishnu marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
return True | ||||||
Bhargavishnu marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
else: | ||||||
return False | ||||||
return False | ||||||
|
||||||
|
||||||
def luhn_validation(credit_card_number: str) -> bool: | ||||||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
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]) | ||||||
|
||||||
if total % 10 == 0: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Remove the next three lines. |
||||||
return True | ||||||
|
||||||
return False | ||||||
|
||||||
|
||||||
def validate_credit_card_number(number: str): | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: As there is no test file in this pull request nor any test function or class in the file |
||||||
""" | ||||||
Function to validate the given credit card number | ||||||
""" | ||||||
credit_card_number = str(number) | ||||||
if credit_card_number.isdigit(): | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let’s fast fail with guard clauses… https://medium.com/lemon-code/guard-clauses-3bc0cd96a2d3 |
||||||
credit_card_number_length = len(credit_card_number) | ||||||
if credit_card_number_length >= 13 and credit_card_number_length <= 16: | ||||||
if validate_initial_digits(credit_card_number): | ||||||
if luhn_validation(credit_card_number): | ||||||
print(f"Given number({number}) is Valid") | ||||||
else: | ||||||
print(f"Invalid number({number}) given: Invalid Number") | ||||||
else: | ||||||
print(f"Invalid number({number}) given: Check starting number") | ||||||
else: | ||||||
print(f"Invalid number({number}) given: Check number length") | ||||||
else: | ||||||
print( | ||||||
f"Invalid number({number}) given: Contains alphabets or special characters" | ||||||
) | ||||||
|
||||||
|
||||||
if __name__ == "__main__": | ||||||
import doctest | ||||||
|
||||||
doctest.testmod() |
Uh oh!
There was an error while loading. Please reload this page.