diff --git a/DIRECTORY.md b/DIRECTORY.md index 447a8c6df311..9a957cb3f39e 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -1351,6 +1351,7 @@ * [Capitalize](strings/capitalize.py) * [Check Anagrams](strings/check_anagrams.py) * [Count Vowels](strings/count_vowels.py) + * [Count Vowels Consonants](strings/count_vowels_consonants.py) * [Credit Card Validator](strings/credit_card_validator.py) * [Damerau Levenshtein Distance](strings/damerau_levenshtein_distance.py) * [Detecting English Programmatically](strings/detecting_english_programmatically.py) diff --git a/searches/hill_climbing.py b/searches/hill_climbing.py index 689b7e5cca8f..8e77e4de35cd 100644 --- a/searches/hill_climbing.py +++ b/searches/hill_climbing.py @@ -1,5 +1,6 @@ # https://en.wikipedia.org/wiki/Hill_climbing import math +from collections.abc import Callable class SearchProblem: @@ -8,7 +9,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 | float], + ) -> None: """ The constructor of the search problem. @@ -22,7 +29,7 @@ def __init__(self, x: int, y: int, step_size: int, function_to_optimize): 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): @@ -34,7 +41,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 +65,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 +79,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 +91,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,