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

2
  • 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 ] Commented Sep 28, 2022 at 17:52
  • Does this answer your question? Why does my recursive function return None? Commented Sep 28, 2022 at 19:08

1 Answer 1

3

add return before every call to the find function

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.