Tried to make a binary search algorithm, how did I do?
def homogeneous_type(list1):
i_list = iter(list1)
listtype = type(next(i_list))
return True if all(isinstance(x, listtype) for x in i_list) else False
def binSearch(list1, value):
if not list1:
raise ValueError("list is empty")
if not homogeneous_type(list1):
raise ValueError("list must be one type")
left = 0
right = len(list1) - 1
list1 = sorted(list1)
while left <= right:
midIndex, mod = divmod(left + right, 2)
midIndex = midIndex + 1 if mod else midIndex
if list1[midIndex] == value:
return midIndex
elif list1[midIndex] < value:
left = midIndex + 1
elif list1[midIndex] > value:
right = midIndex - 1
raise ValueError("value not in List")
homogenous_type
taken from stackoverflow.com/a/13252348/1190388 ? If so, give credits as such! \$\endgroup\$"""Taken from the discussion at: <link>..."""
\$\endgroup\$