Skip to content

Commit fff3acb

Browse files
anandvenugopal-techpre-commit-ci[bot]cclauss
authored
Add missing type hints to hill_climbing.py (#14260)
* 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. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix unnecessary empty iterable in deque initialization (RUF037) * Update type hint for objective function to allow float return values * 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. * Initialize deque with empty list for None values * updating DIRECTORY.md --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com> Co-authored-by: cclauss <cclauss@users.noreply.github.com>
1 parent 752431f commit fff3acb

2 files changed

Lines changed: 15 additions & 7 deletions

File tree

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,6 +1351,7 @@
13511351
* [Capitalize](strings/capitalize.py)
13521352
* [Check Anagrams](strings/check_anagrams.py)
13531353
* [Count Vowels](strings/count_vowels.py)
1354+
* [Count Vowels Consonants](strings/count_vowels_consonants.py)
13541355
* [Credit Card Validator](strings/credit_card_validator.py)
13551356
* [Damerau Levenshtein Distance](strings/damerau_levenshtein_distance.py)
13561357
* [Detecting English Programmatically](strings/detecting_english_programmatically.py)

searches/hill_climbing.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# https://en.wikipedia.org/wiki/Hill_climbing
22
import math
3+
from collections.abc import Callable
34

45

56
class SearchProblem:
@@ -8,7 +9,13 @@ class SearchProblem:
89
The interface will be illustrated using the example of mathematical function.
910
"""
1011

11-
def __init__(self, x: int, y: int, step_size: int, function_to_optimize):
12+
def __init__(
13+
self,
14+
x: int,
15+
y: int,
16+
step_size: int,
17+
function_to_optimize: Callable[[int, int], int | float],
18+
) -> None:
1219
"""
1320
The constructor of the search problem.
1421
@@ -22,7 +29,7 @@ def __init__(self, x: int, y: int, step_size: int, function_to_optimize):
2229
self.step_size = step_size
2330
self.function = function_to_optimize
2431

25-
def score(self) -> int:
32+
def score(self) -> int | float:
2633
"""
2734
Returns the output of the function called with current x and y coordinates.
2835
>>> def test_function(x, y):
@@ -34,7 +41,7 @@ def score(self) -> int:
3441
"""
3542
return self.function(self.x, self.y)
3643

37-
def get_neighbors(self):
44+
def get_neighbors(self) -> list["SearchProblem"]:
3845
"""
3946
Returns a list of coordinates of neighbors adjacent to the current coordinates.
4047
@@ -58,21 +65,21 @@ def get_neighbors(self):
5865
)
5966
]
6067

61-
def __hash__(self):
68+
def __hash__(self) -> int:
6269
"""
6370
hash the string representation of the current search state.
6471
"""
6572
return hash(str(self))
6673

67-
def __eq__(self, obj):
74+
def __eq__(self, obj: object) -> bool:
6875
"""
6976
Check if the 2 objects are equal.
7077
"""
7178
if isinstance(obj, SearchProblem):
7279
return hash(str(self)) == hash(str(obj))
7380
return False
7481

75-
def __str__(self):
82+
def __str__(self) -> str:
7683
"""
7784
string representation of the current search state.
7885
>>> str(SearchProblem(0, 0, 1, None))
@@ -84,7 +91,7 @@ def __str__(self):
8491

8592

8693
def hill_climbing(
87-
search_prob,
94+
search_prob: SearchProblem,
8895
find_max: bool = True,
8996
max_x: float = math.inf,
9097
min_x: float = -math.inf,

0 commit comments

Comments
 (0)