递归深度问题 [英] recursion depth problem

查看:65
本文介绍了递归深度问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我有一个模仿二进制计数的小函数。它运行良好,因为输入不是太长,但是如果我给它的输入时间超过

8个字符它会给出


RuntimeError:在cmp中超出了最大递归深度


递归吗?


================== br />

导入系统

def ch4(item,n = 0):

如果n< len(item):

如果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 ]))


==================


此函数需要输入一串零的形式,比如

这个:


python test-bin.py 00000000


并且预计会输出这样的排列列表:


$ python test-bin.py 0000

1000

0100

1100

0010

1010

0110

1110

0001

1001

0101

1101

0011

1011

0111

1111

感谢您的帮助!


真诚,

监考人员

解决方案

python tes t-bin.py 0000

1000

0100

1100

0010

1010

0110

1110

0001

1001

0101

1101

0011

1011

0111

1111


感谢所有人的帮助!


真诚,

监察员




2007年4月22日下午1点49分,proctor写道:


i有一个模仿二进制计数的小函数。它运行良好,因为输入不是太长,但是如果我给它的输入时间超过

8个字符它会给出


RuntimeError:在cmp中超出了最大递归深度


递归吗?


================== br />

导入系统

def ch4(item,n = 0):

如果n< len(item):

如果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 ]))


==================



是。在该代码中确实存在*那么多递归。 502级

,输入长度为8个字符,1013个,9个字符,2035个,

10个字符......那里有模式;-)


4月22日下午1点24分,Michael Bentley< mich ... @ jedimindworks.comwrote:


2007年4月22日下午1点49分,proctor写道:


i有一个模仿二进制计数的小函数。它运行良好,因为输入不是太长,但是如果我输入的时间超过

8个字符,它会给出

< blockquote class =post_quotes>
RuntimeError:cmp
中超出的最大递归深度


我不太清楚我在做什么不当。在这段代码中真的有很多

递归吗?


==================


import sys


def ch4(item,n = 0):

if n < len(item):

如果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]))


==================



是的。在该代码中确实存在*那么多递归。 502级

输入长度为8个字符,1013个包含9个字符,2035个包含

10个字符......那里有模式;-)



好​​的,谢谢迈克尔!


有没有办法在这里避免它?我怎么能写得更好,(如果

)?


proctor。

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

解决方案

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



On Apr 22, 2007, at 1:49 PM, proctor wrote:

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]))

==================


Yes. There really is *that* much recursion in that code. 502 levels
with input length of 8 characters, 1013 with 9 characters, 2035 with
10 characters... There''s a pattern there ;-)


On Apr 22, 1:24 pm, Michael Bentley <mich...@jedimindworks.comwrote:

On Apr 22, 2007, at 1:49 PM, proctor wrote:

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]))

==================


Yes. There really is *that* much recursion in that code. 502 levels
with input length of 8 characters, 1013 with 9 characters, 2035 with
10 characters... There''s a pattern there ;-)

ok, thanks michael!

is there a way to avoid it here? how could i write this better, (if
at all)?

proctor.


这篇关于递归深度问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆