recursion depth problem

proctor 12cc104 at gmail.com
Sun Apr 22 23:08:31 CEST 2007


On Apr 22, 2:55 pm, half.ital... at gmail.com wrote:
> On Apr 22, 11:49 am, proctor <12cc... at gmail.com> wrote:
>
>
>
> > hello,
>
> > i have a small function which mimics binary counting.  it runs fine as
> > long as the input is not too long, but if i give it input longer than
> > 8 characters it gives
>
> > RuntimeError: maximum recursion depth exceeded in cmp
>
> > i'm not too sure what i am doing improperly.  is there really a lot of
> > recursion in this code?
>
> > ==================
>
> > 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]))
>
> > ==================
>
> > this function expects input in the form of a string of zeros, like
> > this:
>
> > python test-bin.py 00000000
>
> > and is expected to output a list of permutations like this:
>
> > $ python test-bin.py 0000
> > 1000
> > 0100
> > 1100
> > 0010
> > 1010
> > 0110
> > 1110
> > 0001
> > 1001
> > 0101
> > 1101
> > 0011
> > 1011
> > 0111
> > 1111
>
> > thanks for all help!
>
> > sincerely,
> > proctor
>
> If you just want to make it work as is....check
>
> sys.setrecursionlimit()
>
> ~Sean

very nice.  thanks sean.  so is the structure of my original code
unrescuable?  i cannot rearrange it to bypass the recursion?

proctor.




More information about the Python-list mailing list