-2
lister=[4,5,1,2,3,6]

i=0
def Search(arr):
    if arr[i]==3:
        return i
    else:
        if i>=0 and i<=(len(arr)-2):          
            i+1
            return Search(arr)
        else:
            return -1

print(Search(lister))

Linear Search using recursion in python.

I don't know why its not working.

theBeast
  • 3
  • 2
  • 1
    `i+1` does nothing. – AKX Jan 23 '23 at 08:00
  • Does this answer your question? [What is the maximum recursion depth in Python, and how to increase it?](https://stackoverflow.com/questions/3323001/what-is-the-maximum-recursion-depth-in-python-and-how-to-increase-it) – Domenico Jan 23 '23 at 08:02
  • 1
    This may be interesting from a purely academic perspective but is not a good technique for performing a linear search. The maximum recursion depth in Python is ~1000. Therefore if your list is longer than the recursion maxima **and** the item is not found before that limit is reached then it will always fail – DarkKnight Jan 23 '23 at 08:19

3 Answers3

2

There are 2 main issues.
The first is that you're not incrementing i anywhere. i+1 doesn't modify i, just returns the value of i+1 which you're not placing anywhere. To increment i you should use i += 1.
The second is you're not using the externally defined i variable in your Search function.

To fix this you could either declare it as global in the start of your method, or pass it as a parameter to your function.

Global method (less preferable):

lister=[4,5,1,2,3,6]

i=0
def Search(arr):
    global i
    if arr[i]==3:
        return i
    else:
        if i>=0 and i<=(len(arr)-2):          
            i+=1
            return Search(arr)
        else:
            return -1

print(Search(lister))

And the parameter method (which would be preferable):

lister=[4,5,1,2,3,6]

def Search(arr, i=0):
    if arr[i]==3:
        return i
    else:
        if i>=0 and i<=(len(arr)-2):          
            i+=1
            return Search(arr, i)
        else:
            return -1

print(Search(lister))
Adid
  • 1,504
  • 3
  • 13
  • Now this is the complete answer imho +1. Global is a bad idea (it will work, but strongly discouraged), the 2nd solution is the one to go with. To the OP you could read about `assignment` and scopes `LEGB` in python (if you still had doubts and wanted to find out in depth why your solution didn't work) – Gameplay Jan 23 '23 at 08:05
0

You are not incrementing i variable. You have i+1 but you don't assign it to i. You should have i=i+1 or i+=1 for short. This recursive call function is looping infinitely, since i doesn't change.

Alexsen
  • 136
  • 1
  • 6
0

Rather than explicitly testing that the index is in range, why not use exception handling? You can also manage the any potential recursion "overflow" in this manner. For example:

def rfind(_list, val, idx=0):
    try:
        if _list[idx] == val:
            return idx
        return rfind(_list, val, idx+1)
    except (IndexError, RecursionError):
        return -1


print(rfind([1, 2, 3], 2))
print(rfind([1, 2, 3], 4)) # will return None due to IndexError
print(rfind([], 4)) # will return None due to IndexError
print(rfind([0]*2000, 1)) # will return None due to RecursionError

Output:

1
-1
-1
-1
DarkKnight
  • 19,739
  • 3
  • 6
  • 22