From 25675620e902007a7e3ca457d9d39a456ff6a496 Mon Sep 17 00:00:00 2001 From: QuantumNovice <43876848+QuantumNovice@users.noreply.github.com> Date: Sun, 6 Jun 2021 11:00:08 +0500 Subject: [PATCH 1/7] 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 --- hashes/luhn.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 hashes/luhn.py diff --git a/hashes/luhn.py b/hashes/luhn.py new file mode 100644 index 000000000000..05e5da06e598 --- /dev/null +++ b/hashes/luhn.py @@ -0,0 +1,43 @@ +""" Luhn Algorithm """ +from typing import List + +def is_luhn(string: str) -> bool: + ''' + 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 + * + >>> test_cases = [79927398710, 79927398711, 79927398712, 79927398713, + ... 79927398714, 79927398715, 79927398716, 79927398717, 79927398718, 79927398719] + >>> test_cases = list(map(str, test_cases)) + >>> list(map(is_luhn, test_cases)) + [False, False, False, True, False, False, False, False, False, False] + ''' + check_digit: int + vector: List[str] = list(string) + vector, check_digit = vector[:-1], int(vector[-1]) + vector: List[int] = [*map(int, vector)] + + vector.reverse() + for idx, i in enumerate(vector): + + if idx & 1 == 0: + doubled: int = vector[idx] * 2 + if doubled > 9: + doubled -= 9 + + check_digit += doubled + else: + check_digit += i + + if (check_digit) % 10 == 0: + return True + return False + + +if __name__ == "__main__": + import doctest + doctest.testmod() + assert is_luhn('79927398713') From 430fa3a227a5769bd89ed02496b3627c814c08cd Mon Sep 17 00:00:00 2001 From: QuantumNovice <43876848+QuantumNovice@users.noreply.github.com> Date: Sun, 6 Jun 2021 11:03:40 +0500 Subject: [PATCH 2/7] Update DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 9905753b2d24..c87a7be883ed 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -146,6 +146,7 @@ * Number Theory * [Prime Numbers](https://github.com/TheAlgorithms/Python/blob/master/data_structures/hashing/number_theory/prime_numbers.py) * [Quadratic Probing](https://github.com/TheAlgorithms/Python/blob/master/data_structures/hashing/quadratic_probing.py) + * [Luhn's Algorithm](https://github.com/TheAlgorithms/Python/blob/master/data_structures/hashing/luhn.py) * Heap * [Binomial Heap](https://github.com/TheAlgorithms/Python/blob/master/data_structures/heap/binomial_heap.py) * [Heap](https://github.com/TheAlgorithms/Python/blob/master/data_structures/heap/heap.py) From 0158e7119008334562d39ecfe5b6282e6e3f94db Mon Sep 17 00:00:00 2001 From: QuantumNovice <43876848+QuantumNovice@users.noreply.github.com> Date: Sun, 6 Jun 2021 11:05:26 +0500 Subject: [PATCH 3/7] Update luhn.py --- hashes/luhn.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/hashes/luhn.py b/hashes/luhn.py index 05e5da06e598..713e1b202987 100644 --- a/hashes/luhn.py +++ b/hashes/luhn.py @@ -2,7 +2,7 @@ from typing import List def is_luhn(string: str) -> bool: - ''' + """ Perform Luhn validation on input string Algorithm: * Double every other digit starting from 2nd last digit. @@ -14,7 +14,7 @@ def is_luhn(string: str) -> bool: >>> test_cases = list(map(str, test_cases)) >>> list(map(is_luhn, test_cases)) [False, False, False, True, False, False, False, False, False, False] - ''' + """ check_digit: int vector: List[str] = list(string) vector, check_digit = vector[:-1], int(vector[-1]) @@ -40,4 +40,4 @@ def is_luhn(string: str) -> bool: if __name__ == "__main__": import doctest doctest.testmod() - assert is_luhn('79927398713') + assert is_luhn("79927398713") From 48cbd1423750117382fd1a570d32d0ec6ba890a5 Mon Sep 17 00:00:00 2001 From: QuantumNovice <43876848+QuantumNovice@users.noreply.github.com> Date: Sun, 6 Jun 2021 11:06:18 +0500 Subject: [PATCH 4/7] Update luhn.py --- hashes/luhn.py | 1 + 1 file changed, 1 insertion(+) diff --git a/hashes/luhn.py b/hashes/luhn.py index 713e1b202987..c7bb54a02e6a 100644 --- a/hashes/luhn.py +++ b/hashes/luhn.py @@ -4,6 +4,7 @@ def is_luhn(string: str) -> bool: """ Perform Luhn validation on input string + https://en.wikipedia.org/wiki/Luhn_algorithm Algorithm: * Double every other digit starting from 2nd last digit. * Subtract 9 if number is greater than 9. From 895ff808801ff8fbbc1f502b81047b0ee5d24d89 Mon Sep 17 00:00:00 2001 From: QuantumNovice <43876848+QuantumNovice@users.noreply.github.com> Date: Sun, 6 Jun 2021 11:08:52 +0500 Subject: [PATCH 5/7] Update luhn.py --- hashes/luhn.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/hashes/luhn.py b/hashes/luhn.py index c7bb54a02e6a..ac611a6e1648 100644 --- a/hashes/luhn.py +++ b/hashes/luhn.py @@ -1,6 +1,7 @@ """ Luhn Algorithm """ from typing import List + def is_luhn(string: str) -> bool: """ Perform Luhn validation on input string @@ -40,5 +41,6 @@ def is_luhn(string: str) -> bool: if __name__ == "__main__": import doctest + doctest.testmod() assert is_luhn("79927398713") From 840e8ddfe3b5096daef3424cae4f5389c23182f6 Mon Sep 17 00:00:00 2001 From: QuantumNovice <43876848+QuantumNovice@users.noreply.github.com> Date: Tue, 8 Jun 2021 18:50:52 +0500 Subject: [PATCH 6/7] Update luhn.py --- hashes/luhn.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/hashes/luhn.py b/hashes/luhn.py index ac611a6e1648..69e7b4ccf59b 100644 --- a/hashes/luhn.py +++ b/hashes/luhn.py @@ -5,22 +5,22 @@ def is_luhn(string: str) -> bool: """ Perform Luhn validation on input string - https://en.wikipedia.org/wiki/Luhn_algorithm Algorithm: * Double every other digit starting from 2nd last digit. * Subtract 9 if number is greater than 9. * Sum the numbers * >>> test_cases = [79927398710, 79927398711, 79927398712, 79927398713, - ... 79927398714, 79927398715, 79927398716, 79927398717, 79927398718, 79927398719] + ... 79927398714, 79927398715, 79927398716, 79927398717, 79927398718, + ... 79927398719] >>> test_cases = list(map(str, test_cases)) >>> list(map(is_luhn, test_cases)) [False, False, False, True, False, False, False, False, False, False] """ check_digit: int - vector: List[str] = list(string) - vector, check_digit = vector[:-1], int(vector[-1]) - vector: List[int] = [*map(int, vector)] + _vector: List[str] = list(string) + __vector, check_digit = _vector[:-1], int(_vector[-1]) + vector: List[int] = [*map(int, __vector)] vector.reverse() for idx, i in enumerate(vector): @@ -41,6 +41,6 @@ def is_luhn(string: str) -> bool: if __name__ == "__main__": import doctest - + doctest.testmod() assert is_luhn("79927398713") From 470e3ee2ddaeeab53b2611b95c6d6cf94fbf1455 Mon Sep 17 00:00:00 2001 From: QuantumNovice <43876848+QuantumNovice@users.noreply.github.com> Date: Sun, 13 Jun 2021 11:47:55 +0500 Subject: [PATCH 7/7] Update DIRECTORY.md --- DIRECTORY.md | 1 - 1 file changed, 1 deletion(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index c87a7be883ed..9905753b2d24 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -146,7 +146,6 @@ * Number Theory * [Prime Numbers](https://github.com/TheAlgorithms/Python/blob/master/data_structures/hashing/number_theory/prime_numbers.py) * [Quadratic Probing](https://github.com/TheAlgorithms/Python/blob/master/data_structures/hashing/quadratic_probing.py) - * [Luhn's Algorithm](https://github.com/TheAlgorithms/Python/blob/master/data_structures/hashing/luhn.py) * Heap * [Binomial Heap](https://github.com/TheAlgorithms/Python/blob/master/data_structures/heap/binomial_heap.py) * [Heap](https://github.com/TheAlgorithms/Python/blob/master/data_structures/heap/heap.py)