codeperfectplus / Sanatio

Document/String/Number/Date/Email/Username/Password Validation In python.
https://sanatio.readthedocs.io/en/latest/
Apache License 2.0
7 stars 4 forks source link

Luhn algorithm calculated in reverse #10

Closed troyfigiel closed 1 month ago

troyfigiel commented 3 months ago

When looking at the definition of LuhnAlgorithm, I noticed the multiplication of digits in the number by the alternating pattern of 2 and 1 starts from the left. The Luhn algorithm actually starts this multiplication from the right instead. Since the test case has an odd length (excluding the checksum digit), it does not capture this difference. For example, 109 should be valid, but instead LuhnAlgorithm("108").verify() returns True. This can be fixed by replacing remaining_numbers by reversed(remaining_numbers) in the implementation.

codeperfectplus commented 3 months ago

You're right, Great catch! This fix would address the issue and make the LuhnAlgorithm function accurate for both even and odd-length numbers. Do you want to create PR ?