-
-
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
Add credit card string validator #5583
Conversation
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.
Click here to look at the relevant links ⬇️
🔗 Relevant Links
Repository:
Python:
Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.
algorithms-keeper
commands and options
algorithms-keeper actions can be triggered by commenting on this PR:
@algorithms-keeper review
to trigger the checks for only added pull request files@algorithms-keeper review-all
to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.
strings/cc_validator.py
Outdated
return False | ||
|
||
|
||
def validate_credit_card_number(number: str): |
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.
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
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.
Click here to look at the relevant links ⬇️
🔗 Relevant Links
Repository:
Python:
Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.
algorithms-keeper
commands and options
algorithms-keeper actions can be triggered by commenting on this PR:
@algorithms-keeper review
to trigger the checks for only added pull request files@algorithms-keeper review-all
to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.
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.
Click here to look at the relevant links ⬇️
🔗 Relevant Links
Repository:
Python:
Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.
algorithms-keeper
commands and options
algorithms-keeper actions can be triggered by commenting on this PR:
@algorithms-keeper review
to trigger the checks for only added pull request files@algorithms-keeper review-all
to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.
strings/cc_validator.py
Outdated
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 comment
The reason will be displayed to describe this comment to others. Learn more.
if total % 10 == 0: | |
return total % 10 == 0 |
Remove the next three lines.
strings/cc_validator.py
Outdated
Invalid number(41111111111111) given: Invalid Number | ||
""" | ||
credit_card_number = str(number) | ||
if credit_card_number.isdigit(): |
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.
Let’s fast fail with guard clauses… https://medium.com/lemon-code/guard-clauses-3bc0cd96a2d3
strings/cc_validator.py
Outdated
@@ -0,0 +1,112 @@ | |||
def validate_initial_digits(credit_card_number: str) -> bool: |
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.
Please rename this file to have a more self-documenting name like strings/credit_card_validator.py
to help people to find it.
strings/cc_validator.py
Outdated
return False | ||
|
||
|
||
def validate_credit_card_number(number: str) -> None: |
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.
Let's have this function print() the answer but also return True or False to the caller.
def validate_credit_card_number(number: str) -> None: | |
def validate_credit_card_number(number: str) -> bool: |
Co-authored-by: Christian Clauss <[email protected]>
…shnu/Python into add_cc_validator_string
Co-authored-by: Christian Clauss <[email protected]>
Co-authored-by: Christian Clauss <[email protected]>
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.
Click here to look at the relevant links ⬇️
🔗 Relevant Links
Repository:
Python:
Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.
algorithms-keeper
commands and options
algorithms-keeper actions can be triggered by commenting on this PR:
@algorithms-keeper review
to trigger the checks for only added pull request files@algorithms-keeper review-all
to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.
strings/credit_card_validator.py
Outdated
credit_card_number_length = len(credit_card_number) | ||
if credit_card_number_length < 13 and credit_card_number_length > 16: |
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...
credit_card_number_length = len(credit_card_number) | |
if credit_card_number_length < 13 and credit_card_number_length > 16: | |
if not 13 < len(credit_card_number) < 16: |
You can use this code to show yourself how it works:
>>> for i in range(10, 20):
... print(i, not 13 < i < 16)
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 ! 😂
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.
This is a great submission! Thanks for doing this.
@cclauss shall i implement this one in JS repo too ? |
Description
Add a validator function to validate the given credit card number.
References
Luhn Algorithm
Checklist:
Fixes: #{$ISSUE_NO}
.