0
def index(L,v)
 ''' Return index of value v in L '''
 pass

I need help with implementing this function using recursion. Really new to recursion stuff so any advice would help!

Note that L is a list. v is a value.

Georgy
13.9k7 gold badges69 silver badges78 bronze badges
asked Jun 26, 2011 at 0:44
0

9 Answers 9

2

That works

def recursive_index(L, v):
 return 0 if L[0] == v else 1 + recursive_index(L[1:], v)

but is pretty stupid (and will only work if the value exists)

You can add if v not in L: return -1 to make it work for any case, but that is even worst.

Do it really has to be recursive?

answered Jun 26, 2011 at 0:56
2
  • what if there are more occurences of value v in the lst? Commented Jun 27, 2011 at 15:33
  • @michelle it'll find the first one, as expected Commented Jun 27, 2011 at 19:47
1

I assume this is homework.

So you need to understand recursion. Here's an example:

def countdown(n):
 if n == 0:
 print "Hello World!"
 else:
 print n
 countdown(n-1)

You need to start with a starting point, in your case it would probably be the 0th element.

You need an end point, which should be the length - 1 or when you find the element.

Simple if else should do here, with a modified version of countdown as above.

answered Jun 26, 2011 at 0:50
2
1

Yet another way:

def rec(l,v, index=0):
 try:
 if l[index] == v:
 return index
 except IndexError:
 return -1 
 return rec(l,v,index+1)
answered Jun 26, 2011 at 1:26
1
  • I hope this is not due to didactical purposes, but why do you subsequently pop the first element off of the list? Since you pass the current index to the function anyway, you can just check the value at the given index. :) Commented Jun 26, 2011 at 1:37
0

Why would someone write recursive code for that??

>>> [1,2,4,8].index(4)
2
answered Jun 26, 2011 at 0:47
0
0
L = [1, 2, 3, 4, 5, 6, 7, 11, 13]
def index(L, v):
 if len(L) == 0:
 return -1000000
 elif L[0] == v:
 return 0
 else:
 return 1 + index(L[1:], v)
print index(L, 7)
print index(L, 13)
print index(L, 100)

* Remote Interpreter Reinitialized *

6

8

-999991

answered Jun 26, 2011 at 0:56
0

Assuming 0 indexing, the following code will return the index of the element if it exists, or -1 if it is not contained in the list:

def index(L, v):
 if L == []:
 return -1
 elif L[0] == v:
 return 0
 rv = index(L[1:], v)
 if rv < 0:
 return rv
 return rv + 1
answered Jun 26, 2011 at 1:06
0

Here a tail recursive version of it:

def indexof(elem, list_):
 return indexof_tailrec(elem, list_, 0)
def indexof_tailrec(elem, list_, index):
 if index >= len(list_):
 return None
 if list_[index] == elem:
 return index
 return indexof_tailrec(elem, list_, index + 1)

Note, however, that Python does not have tail call optimization (at least not as far as I know).

answered Jun 26, 2011 at 1:27
0

A recursive solution to find the first occurence of an element using Binary Search is this -

def Binary_Search(l, val, low, high):
 if low > high:
 return -1
 mid = (low+high)//2
 if len(l) == 1:
 if l[0] == val:
 return 0
 return -1
 if l[mid] == val:
 if mid == 0 or l[mid-1] != l[mid]:
 return mid
 else:
 return Binary_Search(l, val, low, mid-1)
 elif l[mid] < val:
 return Binary_Search(l, val, mid+1, high)
 else:
 return Binary_Search(l, val, low, mid-1)
answered Jan 15, 2021 at 21:10
-1
def fi(arr,x):
 if len(arr)==0:
 return -1
 elif arr[0]==x:
 return 0
 k= fi(arr[1:],x)
 if k== -1:
 return -1
 else:
 return k+1
answered Jul 27, 2022 at 17:45
2
  • 6
    Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, can you edit your answer to include an explanation of what you're doing and why you believe it is the best approach? Commented Jul 28, 2022 at 1:11
  • 2
    Thank you for editing your answer, but you didn't really improve it. Please re-read Jeremy Caney's comment, especially the bold part. Commented Aug 2, 2022 at 12:43

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.