简体   繁体   English

递归重复输出-Python 3

[英]recursion repeats output - python 3

I'm trying out recursion and am having trouble 我正在尝试递归,但遇到了麻烦

Here's the code: 这是代码:

inputs = []
def ins(x,y,length):
    global inputs, j
    if [x,y] not in inputs:
        inputs.append([x,y])
    if x is not length:
        ins(x+1,y,length)
    if y is not length:
        ins(x,y+1,length)
j = 0
ins(0,0,12)

but it seems to go over the same x,y combo multiple times and gives an inputs ends up being 10400599 items long instead of just 169 但它似乎多次遍历同一个x,y组合,并且输入最终是10400599个项目,而不是169个

my bad all i had to do was change: 我所要做的就是改变:

if y is not length:

to: 至:

elif y is not length:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM