Recursive function

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Mar 2007
Posts: 1
Reputation: cmpt is an unknown quantity at this point 
Solved Threads: 0
cmpt cmpt is offline Offline
Newbie Poster

Recursive function

 
0
  #1
Mar 30th, 2007
search(l, key):

if len(l) == 0:
return False
elif l[0] == key:
return True
else:
return search(l[1:], key)

I was just wondering if there is any way that i can get rid of "elif" statement. The function is suppose to return False if the key is not in the list "l" and True if otherwise. (it has to be a recursive function though)

And plus...
How would I modify this function so that it returns the index of key in l instead of True? (it still returns False if key is not in l)


I am not very good with computer programming...please someone help me? It would be greatly appreciated. =)
Thanks!
Last edited by cmpt; Mar 30th, 2007 at 8:38 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 133
Reputation: mawe is an unknown quantity at this point 
Solved Threads: 58
mawe mawe is offline Offline
Junior Poster

Re: Recursive function

 
1
  #2
Mar 31st, 2007
Hi!

The first one without any if-elif:
  1. def search(l, key):
  2. return key in l
For the second one, .index() is your friend
  1. def search(l, key):
  2. if key in l: return l.index(key)
  3. return False

EDIT: Oh sorry, I didn't see it had to be recursive.
  1. def search(l, key, index=0):
  2. if l:
  3. if l[0] == key: return index
  4. return search(l[1:], key, index+1)
  5. return False

Regards, mawe
Last edited by mawe; Mar 31st, 2007 at 12:21 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 12
Reputation: docaholic is an unknown quantity at this point 
Solved Threads: 1
docaholic docaholic is offline Offline
Newbie Poster

Re: Recursive function

 
0
  #3
Mar 22nd, 2009
Is there anyway you can do the last code without adding the index=0 to the search function arguments?
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 984
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 222
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Posting Shark

Re: Recursive function

 
1
  #4
Mar 23rd, 2009
Yes there is
  1. def search(l, key):
  2. if l:
  3. if l[0] == key:
  4. return 0
  5. s = search(l[1:], key)
  6. if s is not False:
  7. return s + 1
  8. return False
However, the good way to do this is to use the builtin index function !
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 12
Reputation: docaholic is an unknown quantity at this point 
Solved Threads: 1
docaholic docaholic is offline Offline
Newbie Poster

Re: Recursive function

 
0
  #5
Mar 23rd, 2009
dumb question but...what does if l: do?
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 950
Reputation: Paul Thompson has a spectacular aura about Paul Thompson has a spectacular aura about 
Solved Threads: 147
Sponsor
Paul Thompson's Avatar
Paul Thompson Paul Thompson is offline Offline
previously paulthom12345

Re: Recursive function

 
1
  #6
Mar 23rd, 2009
It checks to see if l is existant, if there are no elelments in l then it will return False because your key cant be in an empty list! Therefore avoiding any unwanted Exceptions!
Make it idiot proof and someone will make a better idiot.
Check out my Site | and join us on IRC | Python Specific IRC
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,146
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 949
Moderator
vegaseat's Avatar
vegaseat vegaseat is online now Online
DaniWeb's Hypocrite

Re: Recursive function

 
1
  #7
Mar 23rd, 2009
BTW, the letter 'l' is a rather poor choice for a variable name, since it looks much like the number '1' on many editors. I have gotten in the habit to use 'mylist' or 'qq' for casual lists.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 4227 | Replies: 6
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC