From 0324e6098d1e44e52b9a12efd64fb72264fef513 Mon Sep 17 00:00:00 2001 From: umutKaracelebi Date: Mon, 9 Mar 2026 06:18:15 +0300 Subject: [PATCH 01/11] style: add type hints to matrix_exponentiation.py (#14288) * style: add type hints to matrix_exponentiation.py * Refactor kth_permutation and fix linter errors * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Delete machine_learning/linear_discriminant_analysis.py * Revert "Delete machine_learning/linear_discriminant_analysis.py" This reverts commit de2964731113d240db9d4e5451e2a51b48cc6ace. * Update linear_discriminant_analysis.py * Update kth_lexicographic_permutation.py * Update matrix_exponentiation.py * Update matrix_exponentiation.py --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Maxim Smolskiy --- maths/matrix_exponentiation.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/maths/matrix_exponentiation.py b/maths/matrix_exponentiation.py index 7cdac9d34..15b0c96e0 100644 --- a/maths/matrix_exponentiation.py +++ b/maths/matrix_exponentiation.py @@ -11,7 +11,7 @@ https://www.hackerearth.com/practice/notes/matrix-exponentiation-1/ class Matrix: - def __init__(self, arg): + def __init__(self, arg: list[list] | int) -> None: if isinstance(arg, list): # Initializes a matrix identical to the one provided. self.t = arg self.n = len(arg) @@ -19,7 +19,7 @@ class Matrix: self.n = arg self.t = [[0 for _ in range(self.n)] for _ in range(self.n)] - def __mul__(self, b): + def __mul__(self, b: Matrix) -> Matrix: matrix = Matrix(self.n) for i in range(self.n): for j in range(self.n): @@ -28,7 +28,7 @@ class Matrix: return matrix -def modular_exponentiation(a, b): +def modular_exponentiation(a: Matrix, b: int) -> Matrix: matrix = Matrix([[1, 0], [0, 1]]) while b > 0: if b & 1: @@ -38,7 +38,7 @@ def modular_exponentiation(a, b): return matrix -def fibonacci_with_matrix_exponentiation(n, f1, f2): +def fibonacci_with_matrix_exponentiation(n: int, f1: int, f2: int) -> int: """ Returns the nth number of the Fibonacci sequence that starts with f1 and f2 @@ -64,7 +64,7 @@ def fibonacci_with_matrix_exponentiation(n, f1, f2): return f2 * matrix.t[0][0] + f1 * matrix.t[0][1] -def simple_fibonacci(n, f1, f2): +def simple_fibonacci(n: int, f1: int, f2: int) -> int: """ Returns the nth number of the Fibonacci sequence that starts with f1 and f2 @@ -95,7 +95,7 @@ def simple_fibonacci(n, f1, f2): return f2 -def matrix_exponentiation_time(): +def matrix_exponentiation_time() -> float: setup = """ from random import randint from __main__ import fibonacci_with_matrix_exponentiation @@ -106,7 +106,7 @@ from __main__ import fibonacci_with_matrix_exponentiation return exec_time -def simple_fibonacci_time(): +def simple_fibonacci_time() -> float: setup = """ from random import randint from __main__ import simple_fibonacci @@ -119,7 +119,7 @@ from __main__ import simple_fibonacci return exec_time -def main(): +def main() -> None: matrix_exponentiation_time() simple_fibonacci_time() From 81fcb90f7b7f88bb4b9bbc1f85967b5f26840fda Mon Sep 17 00:00:00 2001 From: Aarav Arya <159852168+Aarav-Arya@users.noreply.github.com> Date: Sun, 8 Mar 2026 23:32:52 -0400 Subject: [PATCH 02/11] Add type hints for bogo_sort.py (#14306) * Added type hints for bogo_sort * Update bogo_sort.py --------- Co-authored-by: Maxim Smolskiy --- sorts/bogo_sort.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sorts/bogo_sort.py b/sorts/bogo_sort.py index 9c133f0d8..70785140e 100644 --- a/sorts/bogo_sort.py +++ b/sorts/bogo_sort.py @@ -16,7 +16,7 @@ python bogo_sort.py import random -def bogo_sort(collection): +def bogo_sort(collection: list) -> list: """Pure implementation of the bogosort algorithm in Python :param collection: some mutable ordered collection with heterogeneous comparable items inside @@ -30,7 +30,7 @@ def bogo_sort(collection): [-45, -5, -2] """ - def is_sorted(collection): + def is_sorted(collection: list) -> bool: for i in range(len(collection) - 1): if collection[i] > collection[i + 1]: return False From 84b59c878186b7ed9e713f7409ccbf0473e3bc0c Mon Sep 17 00:00:00 2001 From: Yaadhuu Date: Mon, 9 Mar 2026 09:38:51 +0530 Subject: [PATCH 03/11] 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 --- maths/greatest_common_divisor.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/maths/greatest_common_divisor.py b/maths/greatest_common_divisor.py index a2174a8eb..1fc123fc2 100644 --- a/maths/greatest_common_divisor.py +++ b/maths/greatest_common_divisor.py @@ -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 From 6f9f4318af4e93a0f7eaa4a2c3dfe698e0d31f6f Mon Sep 17 00:00:00 2001 From: Kalyani Date: Mon, 9 Mar 2026 09:45:03 +0530 Subject: [PATCH 04/11] all "is" replaced with "==" (#14209) Co-authored-by: Maxim Smolskiy --- strings/palindrome.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/strings/palindrome.py b/strings/palindrome.py index e765207e5..4df5639b0 100644 --- a/strings/palindrome.py +++ b/strings/palindrome.py @@ -15,14 +15,14 @@ test_data = { "AB": False, } # Ensure our test data is valid -assert all((key == key[::-1]) is value for key, value in test_data.items()) +assert all((key == key[::-1]) == value for key, value in test_data.items()) def is_palindrome(s: str) -> bool: """ Return True if s is a palindrome otherwise return False. - >>> all(is_palindrome(key) is value for key, value in test_data.items()) + >>> all(is_palindrome(key) == value for key, value in test_data.items()) True """ @@ -41,7 +41,7 @@ def is_palindrome_traversal(s: str) -> bool: """ Return True if s is a palindrome otherwise return False. - >>> all(is_palindrome_traversal(key) is value for key, value in test_data.items()) + >>> all(is_palindrome_traversal(key) == value for key, value in test_data.items()) True """ end = len(s) // 2 @@ -60,7 +60,7 @@ def is_palindrome_recursive(s: str) -> bool: """ Return True if s is a palindrome otherwise return False. - >>> all(is_palindrome_recursive(key) is value for key, value in test_data.items()) + >>> all(is_palindrome_recursive(key) == value for key, value in test_data.items()) True """ if len(s) <= 1: @@ -75,14 +75,14 @@ def is_palindrome_slice(s: str) -> bool: """ Return True if s is a palindrome otherwise return False. - >>> all(is_palindrome_slice(key) is value for key, value in test_data.items()) + >>> all(is_palindrome_slice(key) == value for key, value in test_data.items()) True """ return s == s[::-1] def benchmark_function(name: str) -> None: - stmt = f"all({name}(key) is value for key, value in test_data.items())" + stmt = f"all({name}(key) == value for key, value in test_data.items())" setup = f"from __main__ import test_data, {name}" number = 500000 result = timeit(stmt=stmt, setup=setup, number=number) @@ -91,8 +91,8 @@ def benchmark_function(name: str) -> None: if __name__ == "__main__": for key, value in test_data.items(): - assert is_palindrome(key) is is_palindrome_recursive(key) - assert is_palindrome(key) is is_palindrome_slice(key) + assert is_palindrome(key) == is_palindrome_recursive(key) + assert is_palindrome(key) == is_palindrome_slice(key) print(f"{key:21} {value}") print("a man a plan a canal panama") From 049a34d62be6abe5ce5dcef9b7c75580c6fd3e8f Mon Sep 17 00:00:00 2001 From: Tithi Joshi Date: Mon, 9 Mar 2026 10:00:39 +0530 Subject: [PATCH 05/11] Refine docstring and simplify reverse_letters implementation (#14205) * docs: refine docstring and simplify reverse_letters implementation Updated the docstring for clarity and improved the logic for reversing words. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update reverse_letters.py --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Maxim Smolskiy --- strings/reverse_letters.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/strings/reverse_letters.py b/strings/reverse_letters.py index 4f73f816b..cd1b7832d 100644 --- a/strings/reverse_letters.py +++ b/strings/reverse_letters.py @@ -1,7 +1,7 @@ def reverse_letters(sentence: str, length: int = 0) -> str: """ Reverse all words that are longer than the given length of characters in a sentence. - If unspecified, length is taken as 0 + If ``length`` is not specified, it defaults to 0. >>> reverse_letters("Hey wollef sroirraw", 3) 'Hey fellow warriors' @@ -13,7 +13,7 @@ def reverse_letters(sentence: str, length: int = 0) -> str: 'racecar' """ return " ".join( - "".join(word[::-1]) if len(word) > length else word for word in sentence.split() + word[::-1] if len(word) > length else word for word in sentence.split() ) From f5c3e7c80821512ee2d47a891b9636d436e76705 Mon Sep 17 00:00:00 2001 From: Anusha <135559258+Anusha-DeviE@users.noreply.github.com> Date: Mon, 9 Mar 2026 10:07:31 +0530 Subject: [PATCH 06/11] Replace assert-based validation with explicit errors in modular_division (#14204) * Improve documentation for linear search algorithm * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Replace asserts with explicit validation in modular_division * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update linear_search.py --------- Co-authored-by: Anusha-DeviE Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Maxim Smolskiy --- maths/modular_division.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/maths/modular_division.py b/maths/modular_division.py index 94f12b3e0..ed4ae6ae8 100644 --- a/maths/modular_division.py +++ b/maths/modular_division.py @@ -28,9 +28,13 @@ def modular_division(a: int, b: int, n: int) -> int: 4 """ - assert n > 1 - assert a > 0 - assert greatest_common_divisor(a, n) == 1 + if n <= 1: + raise ValueError("Modulus n must be greater than 1") + if a <= 0: + raise ValueError("Divisor a must be a positive integer") + if greatest_common_divisor(a, n) != 1: + raise ValueError("a and n must be coprime (gcd(a, n) = 1)") + (_d, _t, s) = extended_gcd(n, a) # Implemented below x = (b * s) % n return x From 1ae906a97bd1245107d61bba06558f9a35222b91 Mon Sep 17 00:00:00 2001 From: Shivang Arya Date: Mon, 9 Mar 2026 10:19:15 +0530 Subject: [PATCH 07/11] Fix incorrect doctest references in fibonacci functions (#14200) * Fix incorrect doctest references in fibonacci functions * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Maxim Smolskiy --- maths/fibonacci.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/maths/fibonacci.py b/maths/fibonacci.py index 71ff479f9..595233cf8 100644 --- a/maths/fibonacci.py +++ b/maths/fibonacci.py @@ -91,15 +91,15 @@ def fib_iterative(n: int) -> list[int]: def fib_recursive(n: int) -> list[int]: """ Calculates the first n (0-indexed) Fibonacci numbers using recursion - >>> fib_iterative(0) + >>> fib_recursive(0) [0] - >>> fib_iterative(1) + >>> fib_recursive(1) [0, 1] - >>> fib_iterative(5) + >>> fib_recursive(5) [0, 1, 1, 2, 3, 5] - >>> fib_iterative(10) + >>> fib_recursive(10) [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55] - >>> fib_iterative(-1) + >>> fib_recursive(-1) Traceback (most recent call last): ... ValueError: n is negative @@ -119,7 +119,7 @@ def fib_recursive(n: int) -> list[int]: >>> fib_recursive_term(-1) Traceback (most recent call last): ... - Exception: n is negative + ValueError: n is negative """ if i < 0: raise ValueError("n is negative") @@ -135,15 +135,15 @@ def fib_recursive(n: int) -> list[int]: def fib_recursive_cached(n: int) -> list[int]: """ Calculates the first n (0-indexed) Fibonacci numbers using recursion - >>> fib_iterative(0) + >>> fib_recursive_cached(0) [0] - >>> fib_iterative(1) + >>> fib_recursive_cached(1) [0, 1] - >>> fib_iterative(5) + >>> fib_recursive_cached(5) [0, 1, 1, 2, 3, 5] - >>> fib_iterative(10) + >>> fib_recursive_cached(10) [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55] - >>> fib_iterative(-1) + >>> fib_recursive_cached(-1) Traceback (most recent call last): ... ValueError: n is negative @@ -176,7 +176,7 @@ def fib_memoization(n: int) -> list[int]: [0, 1, 1, 2, 3, 5] >>> fib_memoization(10) [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55] - >>> fib_iterative(-1) + >>> fib_memoization(-1) Traceback (most recent call last): ... ValueError: n is negative From da6b9e9687148f6f99810f761ac59e45b5c1031c Mon Sep 17 00:00:00 2001 From: Yaadhuu Date: Mon, 9 Mar 2026 10:27:20 +0530 Subject: [PATCH 08/11] Use TypeError for non-string input in count_vowels (#14196) * 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 * Update kth_lexicographic_permutation.py --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Maxim Smolskiy --- 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 8a52b331c..e222d8059 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 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 71c7fc8eed7f119a6749fe65efba15ef63ce2c8f Mon Sep 17 00:00:00 2001 From: JAVED KHAN AHMED <149111497+JavedKhan93@users.noreply.github.com> Date: Mon, 9 Mar 2026 10:39:38 +0530 Subject: [PATCH 09/11] docs: upgrade mypy link to https (#14184) Co-authored-by: JavedKhan93 Co-authored-by: Maxim Smolskiy --- CONTRIBUTING.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 35de0bf75..aa6bff3ad 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -159,7 +159,7 @@ We want your work to be readable by others; therefore, we encourage you to note starting_value = int(input("Please enter a starting value: ").strip()) ``` - The use of [Python type hints](https://docs.python.org/3/library/typing.html) is encouraged for function parameters and return values. Our automated testing will run [mypy](http://mypy-lang.org) so run that locally before making your submission. + The use of [Python type hints](https://docs.python.org/3/library/typing.html) is encouraged for function parameters and return values. Our automated testing will run [mypy](https://mypy-lang.org) so run that locally before making your submission. ```python def sum_ab(a: int, b: int) -> int: From 6da02abdce243c947688ddaf1926c9aacb0cbb7b Mon Sep 17 00:00:00 2001 From: radhikaRM06 Date: Mon, 9 Mar 2026 10:52:38 +0530 Subject: [PATCH 10/11] Add doctests for duplicate and sorted inputs in bubble sort (#14154) Co-authored-by: Maxim Smolskiy --- sorts/bubble_sort.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/sorts/bubble_sort.py b/sorts/bubble_sort.py index 9ec3d5384..b09a2afed 100644 --- a/sorts/bubble_sort.py +++ b/sorts/bubble_sort.py @@ -17,6 +17,12 @@ def bubble_sort_iterative(collection: list[Any]) -> list[Any]: [-45, -5, -2] >>> bubble_sort_iterative([-23, 0, 6, -4, 34]) [-23, -4, 0, 6, 34] + >>> bubble_sort_iterative([1, 2, 3, 4]) + [1, 2, 3, 4] + >>> bubble_sort_iterative([3, 3, 3, 3]) + [3, 3, 3, 3] + >>> bubble_sort_iterative([56]) + [56] >>> bubble_sort_iterative([0, 5, 2, 3, 2]) == sorted([0, 5, 2, 3, 2]) True >>> bubble_sort_iterative([]) == sorted([]) From 589d12972d185872b955633f9e7879fb6ecc4317 Mon Sep 17 00:00:00 2001 From: Parth Pawar Date: Mon, 9 Mar 2026 11:03:34 +0530 Subject: [PATCH 11/11] Fix return type description in bubble_sort.py (#14137) * Fix return type description in bubble_sort.py Better (clearer, standard phrasing): :return: the same collection ordered in ascending order * Update bubble_sort.py --------- Co-authored-by: Maxim Smolskiy --- sorts/bubble_sort.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sorts/bubble_sort.py b/sorts/bubble_sort.py index b09a2afed..77d173290 100644 --- a/sorts/bubble_sort.py +++ b/sorts/bubble_sort.py @@ -6,7 +6,7 @@ def bubble_sort_iterative(collection: list[Any]) -> list[Any]: :param collection: some mutable ordered collection with heterogeneous comparable items inside - :return: the same collection ordered by ascending + :return: the same collection ordered in ascending order Examples: >>> bubble_sort_iterative([0, 5, 2, 3, 2])