Skip to content

[mypy] Type annotations for searches directory #5799

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Nov 9, 2021
2 changes: 1 addition & 1 deletion mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
ignore_missing_imports = True
install_types = True
non_interactive = True
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)
exclude = (matrix_operation.py|other/least_recently_used.py|other/lfu_cache.py|other/lru_cache.py)
3 changes: 2 additions & 1 deletion searches/simulated_annealing.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# https://en.wikipedia.org/wiki/Simulated_annealing
import math
import random
from typing import Any

from .hill_climbing import SearchProblem

Expand All @@ -16,7 +17,7 @@ def simulated_annealing(
start_temperate: float = 100,
rate_of_decrease: float = 0.01,
threshold_temp: float = 1,
) -> SearchProblem:
) -> Any:
"""
Implementation of the simulated annealing algorithm. We start with a given state,
find all its neighbors. Pick a random neighbor, if that neighbor improves the
Expand Down
12 changes: 8 additions & 4 deletions searches/ternary_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ def ite_ternary_search(array: list[int], target: int) -> int:
if right - left < precision:
return lin_search(left, right, array, target)

one_third = (left + right) / 3 + 1
two_third = 2 * (left + right) / 3 + 1
one_third = (left + right) // 3 + 1
two_third = 2 * (left + right) // 3 + 1

if array[one_third] == target:
return one_third
Expand Down Expand Up @@ -138,8 +138,8 @@ def rec_ternary_search(left: int, right: int, array: list[int], target: int) ->
if left < right:
if right - left < precision:
return lin_search(left, right, array, target)
one_third = (left + right) / 3 + 1
two_third = 2 * (left + right) / 3 + 1
one_third = (left + right) // 3 + 1
two_third = 2 * (left + right) // 3 + 1

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


if __name__ == "__main__":
import doctest

doctest.testmod()

user_input = input("Enter numbers separated by comma:\n").strip()
collection = [int(item.strip()) for item in user_input.split(",")]
assert collection == sorted(collection), f"List must be ordered.\n{collection}."
Expand Down