I want to find if a number exists in a sorted array. To be straight an array contains fibonaccci number from 1 to 63. Below is the fibonacci number generator and some of it's output.
stacksize = 10000 # default 128 stack
from functools import lru_cache
@lru_cache(stacksize)
def nthfibonacci(n):
if n <= 1:
return 1
elif n == 2:
return 1
elif n > 2:
return nthfibonacci(n - 2) + nthfibonacci(n - 1)
output = [nthfibonacci(k) for k in range(1,63+1)]
# truncated output: [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610,987,
1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368,.....]
Now I want to find if number 7 exists or not so I used the following code using python bisection module:
from bisect import bisect_left
elem_index = bisect_left(a=output, x=7, lo=0, hi=len(arr) - 1)
# output of elem_index is 5 ???? But it is expected to be len(output)+1, right?
# as we know if element is not found it returns len(array)+1
Again if I just write a simple binary search it gives me correct result as follows:
def binsearch(arr, key):
# arr.sort()
low = 0
high = len(arr) - 1
while low <= high:
mid = (low + high) // 2
if arr[mid] == key:
return mid
else:
if arr[mid] < key:
low = mid + 1
else:
high = mid - 1
return -1
print(binsearch(arr, 7)) # it gives me -1 as expected
So what is happening?