| | |
Recursive function
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Mar 2007
Posts: 1
Reputation:
Solved Threads: 0
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!
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.
•
•
Join Date: Sep 2005
Posts: 133
Reputation:
Solved Threads: 58
Hi!
The first one without any if-elif:
For the second one, .index() is your friend 
EDIT: Oh sorry, I didn't see it had to be recursive.
Regards, mawe
The first one without any if-elif:
Python Syntax (Toggle Plain Text)
def search(l, key): return key in l

Python Syntax (Toggle Plain Text)
def search(l, key): if key in l: return l.index(key) return False
EDIT: Oh sorry, I didn't see it had to be recursive.
Python Syntax (Toggle Plain Text)
def search(l, key, index=0): if l: if l[0] == key: return index return search(l[1:], key, index+1) return False
Regards, mawe
Last edited by mawe; Mar 31st, 2007 at 12:21 am.
Yes there is
However, the good way to do this is to use the builtin index function !
python Syntax (Toggle Plain Text)
def search(l, key): if l: if l[0] == key: return 0 s = search(l[1:], key) if s is not False: return s + 1 return False
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
Check out my Site | and join us on IRC | Python Specific IRC
![]() |
Similar Threads
- Order of a recursive function (C)
- Argorithm: palindromes, write recursive function. (Computer Science)
- recursive function (C)
- Recursive function - checking for palindromes (C)
- recursive function (C++)
Other Threads in the Python Forum
- Previous Thread: Joining an Open source project
- Next Thread: Is one large or a few smaller dictionaries more memory efficient?
Views: 4227 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for Python
accessdenied address ansi backend beginner changecolor class code conversion coordinates copy curves customdialog dan08 dictionary directory dynamic edit examples excel feet file float font format ftp function generator getvalue gui halp homework i/o images import info input ip java line linux list lists loop mouse mysql newb number numbers output panel parsing path port prime print program programming projects py2exe pygame pyqt python queue random rational recursion recursive schedule screensaverloopinactive scrolledtext searchingfile server ssh stamp statictext string strings sudokusolver table terminal text thread threading time tkinter tlapse tuple tutorial type ubuntu unicode url urllib urllib2 variable whileloop windows write wxpython






