From 104311f181286f254b89f1117a5a9f0e6257179f Mon Sep 17 00:00:00 2001 From: ANAND VENUGOPAL Date: Sun, 8 Feb 2026 14:12:53 +0530 Subject: [PATCH 1/5] Add missing type hints to hill_climbing.py This commit adds the missing type annotations to searches/hill_climbing.py. - Added type annotation for function_to_optimize using Callable [[int, int], int] - Added return type hints to get_neighbors, __hash__, __eq__, and __str__ - Added missing type hint for search_prob in hill_climbing() - Improved type clarity while preserving existing logic - Used modern Python type hints (PEP 585) This improves readability and typing consistency across the repository. --- searches/hill_climbing.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/searches/hill_climbing.py b/searches/hill_climbing.py index 689b7e5cca8f..f41599c13d26 100644 --- a/searches/hill_climbing.py +++ b/searches/hill_climbing.py @@ -1,6 +1,6 @@ # https://en.wikipedia.org/wiki/Hill_climbing import math - +from collections.abc import Callable class SearchProblem: """ @@ -8,7 +8,13 @@ class SearchProblem: The interface will be illustrated using the example of mathematical function. """ - def __init__(self, x: int, y: int, step_size: int, function_to_optimize): + def __init__( + self, + x: int, + y: int, + step_size: int, + function_to_optimize: Callable[[int, int], int] + ) -> None: """ The constructor of the search problem. @@ -34,7 +40,7 @@ def score(self) -> int: """ return self.function(self.x, self.y) - def get_neighbors(self): + def get_neighbors(self) -> list["SearchProblem"]: """ Returns a list of coordinates of neighbors adjacent to the current coordinates. @@ -58,13 +64,13 @@ def get_neighbors(self): ) ] - def __hash__(self): + def __hash__(self) -> int: """ hash the string representation of the current search state. """ return hash(str(self)) - def __eq__(self, obj): + def __eq__(self, obj: object) -> bool: """ Check if the 2 objects are equal. """ @@ -72,7 +78,7 @@ def __eq__(self, obj): return hash(str(self)) == hash(str(obj)) return False - def __str__(self): + def __str__(self) -> str: """ string representation of the current search state. >>> str(SearchProblem(0, 0, 1, None)) @@ -84,7 +90,7 @@ def __str__(self): def hill_climbing( - search_prob, + search_prob: SearchProblem, find_max: bool = True, max_x: float = math.inf, min_x: float = -math.inf, From b9cfa26aab5d1c0bea75eb490567a4ad62155ab4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 8 Feb 2026 09:12:59 +0000 Subject: [PATCH 2/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- searches/hill_climbing.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/searches/hill_climbing.py b/searches/hill_climbing.py index f41599c13d26..2f098ad32331 100644 --- a/searches/hill_climbing.py +++ b/searches/hill_climbing.py @@ -2,6 +2,7 @@ import math from collections.abc import Callable + class SearchProblem: """ An interface to define search problems. @@ -9,11 +10,11 @@ class SearchProblem: """ def __init__( - self, - x: int, - y: int, - step_size: int, - function_to_optimize: Callable[[int, int], int] + self, + x: int, + y: int, + step_size: int, + function_to_optimize: Callable[[int, int], int], ) -> None: """ The constructor of the search problem. From 3b17991c6070336fb6f324f39e3cd843ea62a771 Mon Sep 17 00:00:00 2001 From: ANAND VENUGOPAL Date: Sun, 8 Feb 2026 15:02:07 +0530 Subject: [PATCH 3/5] Fix unnecessary empty iterable in deque initialization (RUF037) --- data_structures/hashing/hash_table_with_linked_list.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/hashing/hash_table_with_linked_list.py b/data_structures/hashing/hash_table_with_linked_list.py index f404c5251246..c8dffa30b8e8 100644 --- a/data_structures/hashing/hash_table_with_linked_list.py +++ b/data_structures/hashing/hash_table_with_linked_list.py @@ -8,7 +8,7 @@ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) def _set_value(self, key, data): - self.values[key] = deque([]) if self.values[key] is None else self.values[key] + self.values[key] = deque() if self.values[key] is None else self.values[key] self.values[key].appendleft(data) self._keys[key] = self.values[key] From 5d8edae8de5bc1dcb2f9ade25daaac3994c2dbb0 Mon Sep 17 00:00:00 2001 From: ANAND VENUGOPAL Date: Sun, 8 Feb 2026 17:33:42 +0530 Subject: [PATCH 4/5] Update type hint for objective function to allow float return values --- searches/hill_climbing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/searches/hill_climbing.py b/searches/hill_climbing.py index 2f098ad32331..c1b0af0425dd 100644 --- a/searches/hill_climbing.py +++ b/searches/hill_climbing.py @@ -14,7 +14,7 @@ def __init__( x: int, y: int, step_size: int, - function_to_optimize: Callable[[int, int], int], + function_to_optimize: Callable[[int, int], int | float], ) -> None: """ The constructor of the search problem. From 515bbde54da27c678ee9bc1f03b19de3aa86b77a Mon Sep 17 00:00:00 2001 From: ANAND VENUGOPAL Date: Sun, 8 Feb 2026 17:45:14 +0530 Subject: [PATCH 5/5] Update score() return type to support int | float Mypy reported an incompatible return type because function_to_optimize may return float values. Updated score() return type from int to int | float for full compatibility. --- searches/hill_climbing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/searches/hill_climbing.py b/searches/hill_climbing.py index c1b0af0425dd..8e77e4de35cd 100644 --- a/searches/hill_climbing.py +++ b/searches/hill_climbing.py @@ -29,7 +29,7 @@ def __init__( self.step_size = step_size self.function = function_to_optimize - def score(self) -> int: + def score(self) -> int | float: """ Returns the output of the function called with current x and y coordinates. >>> def test_function(x, y):