From 435f37f5b13b836f8e6fb463fa614dd162013c29 Mon Sep 17 00:00:00 2001 From: yaadhuu Date: Fri, 23 Jan 2026 00:54:58 +0530 Subject: [PATCH 1/8] Use TypeError for non-string input in count_vowels --- strings/count_vowels.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/strings/count_vowels.py b/strings/count_vowels.py index 8a52b331c81b..1d053f8d3d94 100644 --- a/strings/count_vowels.py +++ b/strings/count_vowels.py @@ -22,7 +22,8 @@ def count_vowels(s: str) -> int: 1 """ if not isinstance(s, str): - raise ValueError("Input must be a string") + raise TypeError("Input must be a string") + vowels = "aeiouAEIOU" return sum(1 for char in s if char in vowels) From 38c91dfd9c3aeefa484aceb4f7841fe92f518eed Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 22 Jan 2026 19:44:11 +0000 Subject: [PATCH 2/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- strings/count_vowels.py | 1 - 1 file changed, 1 deletion(-) diff --git a/strings/count_vowels.py b/strings/count_vowels.py index 1d053f8d3d94..e222d80590ff 100644 --- a/strings/count_vowels.py +++ b/strings/count_vowels.py @@ -24,7 +24,6 @@ def count_vowels(s: str) -> int: if not isinstance(s, str): raise TypeError("Input must be a string") - vowels = "aeiouAEIOU" return sum(1 for char in s if char in vowels) From d026807048b036ea077ced502987c373c0e800ff Mon Sep 17 00:00:00 2001 From: yaadhuu Date: Fri, 23 Jan 2026 01:38:02 +0530 Subject: [PATCH 3/8] Fix docstring and improve input validation in kth_lexicographic_permutation --- maths/kth_lexicographic_permutation.py | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/maths/kth_lexicographic_permutation.py b/maths/kth_lexicographic_permutation.py index b85558aca6d4..fdf5d5e5cf66 100644 --- a/maths/kth_lexicographic_permutation.py +++ b/maths/kth_lexicographic_permutation.py @@ -1,10 +1,10 @@ def kth_permutation(k, n): """ Finds k'th lexicographic permutation (in increasing order) of - 0,1,2,...n-1 in O(n^2) time. + 0,1,2,...,n-1 in O(n^2) time. Examples: - First permutation is always 0,1,2,...n + First permutation is always 0,1,2,...,n-1 >>> kth_permutation(0,5) [0, 1, 2, 3, 4] @@ -14,11 +14,20 @@ def kth_permutation(k, n): >>> kth_permutation(10,4) [1, 3, 0, 2] """ - # Factorails from 1! to (n-1)! + # Factorials from 1! to (n-1)! + if not isinstance(k, int) or not isinstance(n, int): + raise TypeError("k and n must be integers") + + if n < 1: + raise ValueError("n must be a positive integer") + factorials = [1] for i in range(2, n): factorials.append(factorials[-1] * i) - assert 0 <= k < factorials[-1] * n, "k out of bounds" + + max_k = factorials[-1] * n # equals n! + if not (0 <= k < max_k): + raise ValueError("k out of bounds") permutation = [] elements = list(range(n)) @@ -26,14 +35,15 @@ def kth_permutation(k, n): # Find permutation while factorials: factorial = factorials.pop() - number, k = divmod(k, factorial) - permutation.append(elements[number]) - elements.remove(elements[number]) - permutation.append(elements[0]) + index, k = divmod(k, factorial) + permutation.append(elements[index]) + elements.pop(index) + permutation.append(elements[0]) return permutation + if __name__ == "__main__": import doctest From 68c846dc0e95cd4abfa659912abaf8e5e28b07d1 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 22 Jan 2026 20:12:01 +0000 Subject: [PATCH 4/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/kth_lexicographic_permutation.py | 1 - 1 file changed, 1 deletion(-) diff --git a/maths/kth_lexicographic_permutation.py b/maths/kth_lexicographic_permutation.py index fdf5d5e5cf66..744ef739d153 100644 --- a/maths/kth_lexicographic_permutation.py +++ b/maths/kth_lexicographic_permutation.py @@ -43,7 +43,6 @@ def kth_permutation(k, n): return permutation - if __name__ == "__main__": import doctest From 7cae6ff23def55ecf353eff27fa901cc9a5cb38c Mon Sep 17 00:00:00 2001 From: yaadhuu Date: Tue, 27 Jan 2026 02:03:57 +0530 Subject: [PATCH 5/8] Handle gcd(0, 0) edge case --- maths/greatest_common_divisor.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/maths/greatest_common_divisor.py b/maths/greatest_common_divisor.py index a2174a8eb74a..e8092dad0c0a 100644 --- a/maths/greatest_common_divisor.py +++ b/maths/greatest_common_divisor.py @@ -30,7 +30,11 @@ def greatest_common_divisor(a: int, b: int) -> int: 3 >>> greatest_common_divisor(-3, -9) 3 + >>> greatest_common_divisor(0, 0) + 0 """ + if a == 0 and b == 0: + return 0 return abs(b) if a == 0 else greatest_common_divisor(b % a, a) @@ -50,8 +54,12 @@ 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. + if x == 0 and y == 0: + return 0 + while y: x, y = y, x % y return abs(x) From 16949aab699834edea558da4e28e521ed7f2e4c5 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Mon, 9 Mar 2026 06:58:42 +0300 Subject: [PATCH 6/8] Update kth_lexicographic_permutation.py --- maths/kth_lexicographic_permutation.py | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/maths/kth_lexicographic_permutation.py b/maths/kth_lexicographic_permutation.py index 744ef739d153..b85558aca6d4 100644 --- a/maths/kth_lexicographic_permutation.py +++ b/maths/kth_lexicographic_permutation.py @@ -1,10 +1,10 @@ def kth_permutation(k, n): """ Finds k'th lexicographic permutation (in increasing order) of - 0,1,2,...,n-1 in O(n^2) time. + 0,1,2,...n-1 in O(n^2) time. Examples: - First permutation is always 0,1,2,...,n-1 + First permutation is always 0,1,2,...n >>> kth_permutation(0,5) [0, 1, 2, 3, 4] @@ -14,20 +14,11 @@ def kth_permutation(k, n): >>> kth_permutation(10,4) [1, 3, 0, 2] """ - # Factorials from 1! to (n-1)! - if not isinstance(k, int) or not isinstance(n, int): - raise TypeError("k and n must be integers") - - if n < 1: - raise ValueError("n must be a positive integer") - + # Factorails from 1! to (n-1)! factorials = [1] for i in range(2, n): factorials.append(factorials[-1] * i) - - max_k = factorials[-1] * n # equals n! - if not (0 <= k < max_k): - raise ValueError("k out of bounds") + assert 0 <= k < factorials[-1] * n, "k out of bounds" permutation = [] elements = list(range(n)) @@ -35,11 +26,11 @@ def kth_permutation(k, n): # Find permutation while factorials: factorial = factorials.pop() - index, k = divmod(k, factorial) - permutation.append(elements[index]) - elements.pop(index) - + number, k = divmod(k, factorial) + permutation.append(elements[number]) + elements.remove(elements[number]) permutation.append(elements[0]) + return permutation From f3daa2a886e335fa090ee62e981002fd5d93fb6b Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Mon, 9 Mar 2026 06:59:03 +0300 Subject: [PATCH 7/8] Update count_vowels.py --- strings/count_vowels.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/strings/count_vowels.py b/strings/count_vowels.py index e222d80590ff..8a52b331c81b 100644 --- a/strings/count_vowels.py +++ b/strings/count_vowels.py @@ -22,7 +22,7 @@ def count_vowels(s: str) -> int: 1 """ if not isinstance(s, str): - raise TypeError("Input must be a string") + raise ValueError("Input must be a string") vowels = "aeiouAEIOU" return sum(1 for char in s if char in vowels) From efd5c5d2b5bd5e4fd69b61a516aad19858144650 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Mon, 9 Mar 2026 07:02:16 +0300 Subject: [PATCH 8/8] Update greatest_common_divisor.py --- maths/greatest_common_divisor.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/maths/greatest_common_divisor.py b/maths/greatest_common_divisor.py index e8092dad0c0a..1fc123fc2b14 100644 --- a/maths/greatest_common_divisor.py +++ b/maths/greatest_common_divisor.py @@ -33,8 +33,6 @@ def greatest_common_divisor(a: int, b: int) -> int: >>> greatest_common_divisor(0, 0) 0 """ - if a == 0 and b == 0: - return 0 return abs(b) if a == 0 else greatest_common_divisor(b % a, a) @@ -57,9 +55,7 @@ def gcd_by_iterative(x: int, y: int) -> int: >>> gcd_by_iterative(0, 0) 0 """ - if x == 0 and y == 0: - return 0 - while y: + while y: # --> when y=0 then loop will terminate and return x as final GCD. x, y = y, x % y return abs(x)