Skip to content

Commit 745f9e2

Browse files
[mypy] Type annotations for searches directory (#5799)
* Update ternary_search.py * Update mypy.ini * Update simulated_annealing.py * Update ternary_search.py * formatting * formatting * Update matrix_operation.py * Update matrix_operation.py * Update matrix_operation.py
1 parent c3d1ff0 commit 745f9e2

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

Diff for: mypy.ini

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
ignore_missing_imports = True
33
install_types = True
44
non_interactive = True
5-
exclude = (matrix_operation.py|other/least_recently_used.py|other/lfu_cache.py|other/lru_cache.py|searches/simulated_annealing.py|searches/ternary_search.py)
5+
exclude = (matrix_operation.py|other/least_recently_used.py|other/lfu_cache.py|other/lru_cache.py)

Diff for: searches/simulated_annealing.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# https://en.wikipedia.org/wiki/Simulated_annealing
22
import math
33
import random
4+
from typing import Any
45

56
from .hill_climbing import SearchProblem
67

@@ -16,7 +17,7 @@ def simulated_annealing(
1617
start_temperate: float = 100,
1718
rate_of_decrease: float = 0.01,
1819
threshold_temp: float = 1,
19-
) -> SearchProblem:
20+
) -> Any:
2021
"""
2122
Implementation of the simulated annealing algorithm. We start with a given state,
2223
find all its neighbors. Pick a random neighbor, if that neighbor improves the

Diff for: searches/ternary_search.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ def ite_ternary_search(array: list[int], target: int) -> int:
8989
if right - left < precision:
9090
return lin_search(left, right, array, target)
9191

92-
one_third = (left + right) / 3 + 1
93-
two_third = 2 * (left + right) / 3 + 1
92+
one_third = (left + right) // 3 + 1
93+
two_third = 2 * (left + right) // 3 + 1
9494

9595
if array[one_third] == target:
9696
return one_third
@@ -138,8 +138,8 @@ def rec_ternary_search(left: int, right: int, array: list[int], target: int) ->
138138
if left < right:
139139
if right - left < precision:
140140
return lin_search(left, right, array, target)
141-
one_third = (left + right) / 3 + 1
142-
two_third = 2 * (left + right) / 3 + 1
141+
one_third = (left + right) // 3 + 1
142+
two_third = 2 * (left + right) // 3 + 1
143143

144144
if array[one_third] == target:
145145
return one_third
@@ -157,6 +157,10 @@ def rec_ternary_search(left: int, right: int, array: list[int], target: int) ->
157157

158158

159159
if __name__ == "__main__":
160+
import doctest
161+
162+
doctest.testmod()
163+
160164
user_input = input("Enter numbers separated by comma:\n").strip()
161165
collection = [int(item.strip()) for item in user_input.split(",")]
162166
assert collection == sorted(collection), f"List must be ordered.\n{collection}."

0 commit comments

Comments
 (0)