From 745f9e2bc37280368ae007d1a30ffc217e4a5b81 Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj Date: Tue, 9 Nov 2021 21:18:30 +0530 Subject: [PATCH] [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 --- mypy.ini | 2 +- searches/simulated_annealing.py | 3 ++- searches/ternary_search.py | 12 ++++++++---- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/mypy.ini b/mypy.ini index ce7c262ab..94fb125fb 100644 --- a/mypy.ini +++ b/mypy.ini @@ -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) diff --git a/searches/simulated_annealing.py b/searches/simulated_annealing.py index 2aa980be7..ad29559f1 100644 --- a/searches/simulated_annealing.py +++ b/searches/simulated_annealing.py @@ -1,6 +1,7 @@ # https://en.wikipedia.org/wiki/Simulated_annealing import math import random +from typing import Any from .hill_climbing import SearchProblem @@ -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 diff --git a/searches/ternary_search.py b/searches/ternary_search.py index 01e437723..9830cce36 100644 --- a/searches/ternary_search.py +++ b/searches/ternary_search.py @@ -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 @@ -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 @@ -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}."