From 435f37f5b13b836f8e6fb463fa614dd162013c29 Mon Sep 17 00:00:00 2001 From: yaadhuu Date: Fri, 23 Jan 2026 00:54:58 +0530 Subject: [PATCH 1/6] 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/6] [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/6] 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/6] [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 9f73cccf04c2a2f4941882270ed43f8949edcdb7 Mon Sep 17 00:00:00 2001 From: yaadhuu Date: Fri, 23 Jan 2026 01:38:02 +0530 Subject: [PATCH 5/6] Fix docstring and improve input validation in kth_lexicographic_permutation --- maths/kth_lexicographic_permutation.py | 1 + 1 file changed, 1 insertion(+) diff --git a/maths/kth_lexicographic_permutation.py b/maths/kth_lexicographic_permutation.py index 744ef739d153..fdf5d5e5cf66 100644 --- a/maths/kth_lexicographic_permutation.py +++ b/maths/kth_lexicographic_permutation.py @@ -43,6 +43,7 @@ def kth_permutation(k, n): return permutation + if __name__ == "__main__": import doctest From ce19ee4f28e4fac662c608c5ac0cdf9a7d24e365 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:33:55 +0000 Subject: [PATCH 6/6] [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