0

I was instructed to run the current code and understand the error message and change only one line of the function bs and fix the error I can kind of understand what the error message is saying, but I don't know how to fix it or where the actual problem is occurring in the function bs

def bs(L, item, left, right):
    if right is None:
        right = len(L)
    if right - left == 0:
        return False
    if right - left == 1:
        return L[left] == item
    median = (right + left)//2
    if item < L[median]:
        return bs(L, item, left, median)
    else:
        return bs(L, item, median, right)

L = [i for i in range(10)]
for i in range(20):
    print(bs(L, i))

The error is the following

    print(bs(L, i))
    TypeError: bs() missing 2 required positional arguments: 'left' and 'right'
Alperen
  • 3,772
  • 3
  • 27
  • 49
Vinay Patel
  • 15
  • 1
  • 6

1 Answers1

1

EDIT

Simply, assign default values to your parameters:(Thanks to trentcl)

def bs(L, item, left = 0, right = None):
...

BEFORE EDIT

If you want to call function like bs(L, i), you need default values in your function. left must be 0 and right must be len(L) by default, but as far as I know, you can't do this in function definition. Change these lines:

sentinel = object()
def bs(L, item, left = 0, right = sentinel):
    if right is sentinel:
        right = len(L)
...

Sentinel is used as a flag or dummy data as usual. Here is an example. If it confuses your mind and you don't want to use it, just use a special integer like -1:

def bs(L, item, left = 0, right = -1):
    if right is -1:
        right = len(L)
...
Alperen
  • 3,772
  • 3
  • 27
  • 49