[Tutor] help with a recursive function
c smith
illusiontechniques at gmail.com
Tue Sep 27 20:32:00 CEST 2011
hi list,
i understand the general idea of recursion and if I am following well
written code I can understand how it works, but when I try to write it for
myself I get a bit confused with the flow.
I was trying to turn an ackerman function into python code for practice and
I tried writing it like this:
#!/usr/bin/env python
import sys, os
def ack(m,n):
if m == 0:
return n+1
elif m > 0 and n == 0:
ack(m-1,1)
elif m > 0 and n > 0:
ack(m-1,ack(m,n-1))
if __name__=='__main__':
sys.argv[1] = int(sys.argv[1])
sys.argv[2] = int(sys.argv[2])
print ack(sys.argv[1], sys.argv[2])
The major problem, I think, is that I cant figure out where to turn the args
into ints.
When run in this form I get the 'typeError' error on 'n+1' I guess because
the args are still strings.
Also, does the second 'elif' make sense?
I am not sure if the function can take itself as an argument.
thanks for any suggestions, this list has been a big help.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110927/d7c4be45/attachment-0001.html>
More information about the Tutor
mailing list