Vishnudas V
Vishnudas V

Reputation: 21

my Binary Search program return None always instead of returning Index value or -1

I am trying to find a target value from a list but its returning None Instead of returning (index value or -1)

class Solution:
    def search(self, nums: List[int], target: int) -> int:
        ub=len(nums)-1
        lb=0
        length=lb+ub
        flag=0
        def find(nums,lb,ub,length,target):
            mid=length//2
            if(lb>ub):
                return -1
            elif(target==nums[mid]):
                return mid
            elif(target<=nums[mid]):
                ub=mid-1
                length=lb+ub
                find(nums,lb,ub,length,target)
            elif(target>=nums[mid]):
                lb=mid+1
                length=lb+ub
                find(nums,lb,ub,length,target)
        find(nums,lb,ub,length,target)
            

Output None

I know there were other solutions but can't understand what causes this error right now

Upvotes: 0

Views: 39

Answers (1)

wiktort1
wiktort1

Reputation: 340

add return before every call to the find function

Upvotes: 3

Related Questions