-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Luhn algorithm #4487
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
Luhn algorithm #4487
Conversation
Perform Luhn validation on input string Algorithm: * Double every other digit starting from 2nd last digit. * Subtract 9 if number is greater than 9. * Sum the numbers https://en.wikipedia.org/wiki/Luhn_algorithm
mypy typing doesn't support mutable static types. I won't be changing that. |
check_digit: int | ||
_vector: List[str] = list(string) | ||
__vector, check_digit = _vector[:-1], int(_vector[-1]) | ||
vector: List[int] = [*map(int, __vector)] |
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.
We can remove the noise of extra variables with similar names here
tokens, check_digit = list(string), int(list(string)[-1])
vector: List[int] = list(map(int, tokens[:-1]))
A variable declaration costs memory, so use it judicially
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.
mypy static checks doesn't pass when "the noise" isn't there
:)
Nice contribution !! |
* Luhn algorithm Perform Luhn validation on input string Algorithm: * Double every other digit starting from 2nd last digit. * Subtract 9 if number is greater than 9. * Sum the numbers https://en.wikipedia.org/wiki/Luhn_algorithm * Update DIRECTORY.md * Update luhn.py * Update luhn.py * Update luhn.py * Update luhn.py * Update DIRECTORY.md
Perform Luhn validation on input string
Algorithm:
* Double every other digit starting from 2nd last digit.
* Subtract 9 if number is greater than 9.
* Sum the numbers
https://en.wikipedia.org/wiki/Luhn_algorithm
Describe your change:
Checklist:
Fixes: #{$ISSUE_NO}
.