Skip to content

Add type hints for searches/ternary_search.py #2874

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 1 commit into from
Oct 5, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions searches/ternary_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,22 @@
Space Complexity : O(1)
"""
import sys
from typing import List

# This is the precision for this function which can be altered.
# It is recommended for users to keep this number greater than or equal to 10.
precision = 10


# This is the linear search that will occur after the search space has become smaller.
def lin_search(left, right, A, target):
def lin_search(left: int, right: int, A: List[int], target: int):
for i in range(left, right + 1):
if A[i] == target:
return i


# This is the iterative method of the ternary search algorithm.
def ite_ternary_search(A, target):
def ite_ternary_search(A: List[int], target: int):
left = 0
right = len(A) - 1
while True:
Expand Down Expand Up @@ -51,7 +52,7 @@ def ite_ternary_search(A, target):


# This is the recursive method of the ternary search algorithm.
def rec_ternary_search(left, right, A, target):
def rec_ternary_search(left: int, right: int, A: List[int], target: int):
if left < right:

if right - left < precision:
Expand All @@ -77,7 +78,7 @@ def rec_ternary_search(left, right, A, target):


# This function is to check if the array is sorted.
def __assert_sorted(collection):
def __assert_sorted(collection: List[int]) -> bool:
if collection != sorted(collection):
raise ValueError("Collection must be sorted")
return True
Expand Down