[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
This commit is contained in:
Rohan R Bharadwaj 2021-11-09 21:18:30 +05:30 committed by GitHub
parent c3d1ff0ebd
commit 745f9e2bc3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 6 deletions

View File

@ -2,4 +2,4 @@
ignore_missing_imports = True ignore_missing_imports = True
install_types = True install_types = True
non_interactive = 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)

View File

@ -1,6 +1,7 @@
# https://en.wikipedia.org/wiki/Simulated_annealing # https://en.wikipedia.org/wiki/Simulated_annealing
import math import math
import random import random
from typing import Any
from .hill_climbing import SearchProblem from .hill_climbing import SearchProblem
@ -16,7 +17,7 @@ def simulated_annealing(
start_temperate: float = 100, start_temperate: float = 100,
rate_of_decrease: float = 0.01, rate_of_decrease: float = 0.01,
threshold_temp: float = 1, threshold_temp: float = 1,
) -> SearchProblem: ) -> Any:
""" """
Implementation of the simulated annealing algorithm. We start with a given state, 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 find all its neighbors. Pick a random neighbor, if that neighbor improves the

View File

@ -89,8 +89,8 @@ def ite_ternary_search(array: list[int], target: int) -> int:
if right - left < precision: if right - left < precision:
return lin_search(left, right, array, target) return lin_search(left, right, array, target)
one_third = (left + right) / 3 + 1 one_third = (left + right) // 3 + 1
two_third = 2 * (left + right) / 3 + 1 two_third = 2 * (left + right) // 3 + 1
if array[one_third] == target: if array[one_third] == target:
return one_third return one_third
@ -138,8 +138,8 @@ def rec_ternary_search(left: int, right: int, array: list[int], target: int) ->
if left < right: if left < right:
if right - left < precision: if right - left < precision:
return lin_search(left, right, array, target) return lin_search(left, right, array, target)
one_third = (left + right) / 3 + 1 one_third = (left + right) // 3 + 1
two_third = 2 * (left + right) / 3 + 1 two_third = 2 * (left + right) // 3 + 1
if array[one_third] == target: if array[one_third] == target:
return one_third return one_third
@ -157,6 +157,10 @@ def rec_ternary_search(left: int, right: int, array: list[int], target: int) ->
if __name__ == "__main__": if __name__ == "__main__":
import doctest
doctest.testmod()
user_input = input("Enter numbers separated by comma:\n").strip() user_input = input("Enter numbers separated by comma:\n").strip()
collection = [int(item.strip()) for item in user_input.split(",")] collection = [int(item.strip()) for item in user_input.split(",")]
assert collection == sorted(collection), f"List must be ordered.\n{collection}." assert collection == sorted(collection), f"List must be ordered.\n{collection}."