mirror of
https://github.com/TheAlgorithms/Python.git
synced 2026-03-13 09:50:19 +08:00
Handle gcd(0, 0) edge case (#14215)
* Use TypeError for non-string input in count_vowels * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix docstring and improve input validation in kth_lexicographic_permutation * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Handle gcd(0, 0) edge case * Update kth_lexicographic_permutation.py * Update count_vowels.py * Update greatest_common_divisor.py --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Maxim Smolskiy <mithridatus@mail.ru>
This commit is contained in:
@@ -30,6 +30,8 @@ def greatest_common_divisor(a: int, b: int) -> int:
|
||||
3
|
||||
>>> greatest_common_divisor(-3, -9)
|
||||
3
|
||||
>>> greatest_common_divisor(0, 0)
|
||||
0
|
||||
"""
|
||||
return abs(b) if a == 0 else greatest_common_divisor(b % a, a)
|
||||
|
||||
@@ -50,6 +52,8 @@ def gcd_by_iterative(x: int, y: int) -> int:
|
||||
1
|
||||
>>> gcd_by_iterative(11, 37)
|
||||
1
|
||||
>>> gcd_by_iterative(0, 0)
|
||||
0
|
||||
"""
|
||||
while y: # --> when y=0 then loop will terminate and return x as final GCD.
|
||||
x, y = y, x % y
|
||||
|
||||
Reference in New Issue
Block a user