Search code examples
pythonclassundefinedbinary-search

Why is my function not defined in this class?


I'm fairly new to creating algorithms and actually using them for problems so I've spent a couple of days learning what binary search is and I created one from scratch that should work. So now I tried to implement it in a leetcode problem called "Search insert position":

Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You must write an algorithm with O(log n) runtime complexity.*

Example 1: Input: nums = [1,3,5,6], target = 5 Output: 2

I've tested my program on my IDE (without the class Solution(object):) and it worked for the test cases provided but for some reason the leetcode IDE is giving me a

NameError: global name 'binary_search' is not defined output = binary_search(nums, target, start, middle, end)

I'm not that familiar with classes so it might be because I didn't do something I should have like adding a def __init__(self, ...), but I thought that it shouldn't matter in leetcode.

Any suggestions or help would be greatly appreciated!

Here is my code:

class Solution(object):
    def binary_search(self, nums, target, start, middle, end):
        if nums[middle] == target:
            return middle
        else:
            while end >= start:
                if nums[middle] > target: # target is smaller than middle index
                    end = middle - 1 # - 1 just incase a value is at index 0
                    middle = (start + end) // 2
                    return(binary_search(nums, target, start, middle, end))
                elif nums[middle] < target: # target is bigger than middle index 
                    start = middle + 1 # + 1 just incase a value is at index [-1] (last index)
                    middle = (start + end) // 2
                    return(binary_search(nums, target, start, middle, end))
        return(middle+1)
    
    def searchInsert(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: int
        """
        start = 0
        end = len(nums)-1
        middle = end // 2 # middle index not actual middle value
        output = binary_search(nums, target, start, middle, end)
        return(output)

Solution

  • You need to use self because binary_search is a class instance attribute in this context. Try this:

    class Solution(object):
        def binary_search(self, nums, target, start, middle, end):
            if nums[middle] == target:
                return middle
            else:
                while end >= start:
                    if nums[middle] > target: # target is smaller than middle index
                        end = middle - 1 # - 1 just incase a value is at index 0
                        middle = (start + end) // 2
                        return(self.binary_search(nums, target, start, middle, end))
                    elif nums[middle] < target: # target is bigger than middle index 
                        start = middle + 1 # + 1 just incase a value is at index [-1] (last index)
                        middle = (start + end) // 2
                        return(self.binary_search(nums, target, start, middle, end))
            return(middle+1)
        
        def searchInsert(self, nums, target):
            """
            :type nums: List[int]
            :type target: int
            :rtype: int
            """
            start = 0
            end = len(nums)-1
            middle = end // 2 # middle index not actual middle value
            output = self.binary_search(nums, target, start, middle, end)
            return(output)