Reputation: 43
I'm trying to create a Binary Search Program, but keep getting this error: TypeError: first argument must be string or compiled pattern.
My method is part of class "Solution" and the first argument is a list of ints. The second argument is an int.
class Solution:
def search(self, nums: List[int], target: int) -> int:
print(type(nums))
if len(nums) == 1:
if nums[0] == target:
return nums.index(target)
else:
return -1
halfValue = len(nums)//2
firstHalf = nums[:halfValue]
secondHalf = nums[halfValue:]
if halfValue > target:
search(self, firstHalf, target)
elif halfValue < target:
search(self, secondHalf, target)
elif halfValue == target:
return nums.index(target)
Upvotes: 0
Views: 573
Reputation: 1580
You're not calling your own search
function, you're calling the re.search
function that LeetCode globally imported for you. Use self.search(firstHalf, target)
instead (and likewise the second case).
(You also have other bugs and nums.index(target)
defeats the purpose of the binary search, but that's not the question here...)
Upvotes: 1