So i tried to do binary search in python, which i think syntactically right, but logically somewhere going wrong, despite of checking it infinite times. Here is my code.
def m(a,ele):
a=a
ele=ele
length = len(a)
if length <=1:
return a
mid = a[length//2]
if ele == mid:
return True
elif ele > mid:
print("if")
if len( a[a.index(mid):]) == 0:
return False
elif len( a[a.index(mid):]) ==1 and ele != a[a.index(mid):]:
return False
elif len( a[a.index(mid):]) ==1 and ele == a[a.index(mid):]:
return True
else:
m( a[a.index(mid):],ele)
else:
print("else")
if len( a[:a.index(mid)]) == 0:
return False
elif len( a[:a.index(mid)]) ==1 and ele != a[:a.index(mid)]:
return False
elif len( a[:a.index(mid)]) ==1 and ele == a[:a.index(mid)]:
return True
else:
m( a[:a.index(mid)],ele)
print(m([1,3,6,8],8))
SO, in this case it should print True but it is printing None. Any help regarding this code would be helpful, i know i am doing some silly mistakes.
return m( a[a.index(mid):],ele)