recursion depth problem

proctor 12cc104 at gmail.com
Mon Apr 23 00:20:42 EDT 2007


On Apr 22, 8:23 pm, a... at mac.com (Alex Martelli) wrote:
> Steven Bethard <steven.beth... at gmail.com> wrote:
>
>    ...
>
>
>
> > >>>>> import sys
> > >>>>> def ch4(item, n=0):
> > >>>>>    if n < len(item):
> > >>>>>            if item[n] == '0':
> > >>>>>                    item[n] = '1'
> > >>>>>                    print ''.join(item)
> > >>>>>                    ch4(item)
> > >>>>>            elif item[n] == '1':
> > >>>>>                    item[n] = '0'
> > >>>>>                    ch4(item, n+1)
> > >>>>> ch4(list(sys.argv[1]))
>    ...
> > > for interest sake:  is my method unredeemable?
>
> > Let's just say that I don't currently see an obvious way of redeeming
> > it. ;-)
>
> Change the outer if into a while, and the recursive calls into proper
> assignments to n.  They're both tail-recursive calls, so this won't
> change the semantics, as it happens.
>
> Alex

really helpful!  thank you very much!

proctor.




More information about the Python-list mailing list