0

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

  • It May be a little funny We can easily solve this problem by four lines of code [ if target in nums: return nums.index(target) elif target not in nums: return -1 ] – Vishnudas V Sep 28 '22 at 17:52
  • Does this answer your question? [Why does my recursive function return None?](https://stackoverflow.com/questions/17778372/why-does-my-recursive-function-return-none) – pjs Sep 28 '22 at 19:08

1 Answers1

3

add return before every call to the find function

wiktort1
  • 330
  • 2
  • 4