Skip to content

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
merged 17 commits into from
Oct 26, 2021
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions strings/cc_validator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
def validate_initial_digits(credit_card_number: str) -> bool:
Copy link
Member

@cclauss cclauss Oct 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please rename this file to have a more self-documenting name like strings/credit_card_validator.py to help people to find it.

first_digit = int(credit_card_number[0])
if first_digit == 4 or first_digit == 5 or first_digit == 6:
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:
return True
else:
return False
return False


def luhn_validation(credit_card_number: str) -> bool:
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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if total % 10 == 0:
return total % 10 == 0

Remove the next three lines.

return True

return False


def validate_credit_card_number(number: str):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: validate_credit_card_number. If the function does not return a value, please provide the type hint as: def function() -> None:

As there is no test file in this pull request nor any test function or class in the file strings/cc_validator.py, please provide doctest for the function validate_credit_card_number

"""
Function to validate the given credit card number
"""
credit_card_number = str(number)
if credit_card_number.isdigit():
Copy link
Member

Choose a reason for hiding this comment

The 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()