简体   繁体   English

递归 - 楼梯问题 - 为什么我的代码不起作用? - Python

[英]Recursion - Stairs Problem - Why doesn't my code work? - Python

Problem:问题:

In this problem, the scenario we are evaluating is the following: You're standing at the base of a staircase and are heading to the top.在此问题中,我们正在评估的场景如下:您站在楼梯底部,正前往顶部。 A small stride will move up one stair, and a large stride advances two.小步将向上移动一个楼梯,大步前进两个。 You want to count the number of ways to climb the entire staircase based on different combinations of large and small strides.你想根据大步和小步的不同组合来计算爬整个楼梯的方法数。 For example, a staircase of three steps can be climbed in three different ways: three small strides, one small stride followed by one large stride, or one large followed by one small.例如,一个三级台阶的楼梯可以用三种不同的方式爬上去:三小步,一小步后一大步,或一大步后一小步。 Write a recursive method waysToClimb that takes a non-negative integer value representing a number of stairs and prints each unique way to climb a staircase of that height, taking strides of one or two stairs at a time.编写一个递归方法waysToClimb ,它采用表示楼梯数量的非负值 integer 并打印爬上该高度楼梯的每种唯一方式,一次走一个或两个楼梯。 Your method should output each way to climb the stairs on its own line, using a 1 to indicate a small stride of 1 stair, and a 2 to indicate a large stride of 2 stairs.你的方法应该是 output 沿着自己的路线爬楼梯,使用 1 表示小步幅 1 级楼梯,使用 2 表示大步幅 2 级楼梯。 For example, the call of waysToClimb(3) should produce the following output:例如,调用waysToClimb(3)应产生以下 output:

[1, 1, 1]
[1, 2]
[2, 1]

My Code:我的代码:

def waysToClimb(n,a=[]):
    if n == 0:
        print(a)
    if n >= 1:
        a.append(1)
        waysToClimb(n-1,a)
    if n >= 2:
        a.append(2)
        waysToClimb(n-2,a)

But for example when I type:但是例如当我输入:

print(waysToClimb(3,[]))

The result is [2, 1] [2, 1, 1, 2] [2, 1, 1, 2, 1, 1] .结果是[2, 1] [2, 1, 1, 2] [2, 1, 1, 2, 1, 1] Why doesn't my code work correctly?为什么我的代码不能正常工作?

EDIT: I was supposed to change "a" variable within function not outside it.编辑:我应该在 function 内而不是在它之外更改“a”变量。 I wrote fct(n,a+[2]) instead of a.append(2) and it worked.我写了 fct(n,a+[2]) 而不是 a.append(2) 并且它起作用了。 Thanks for help all.感谢大家的帮助。

Something, well, differently convoluted:有些东西,嗯,不同的令人费解:

def ways2climb(n,sofar=[]):
    return [sofar] if n==0 else (ways2climb(n-2,sofar+[2]) if n>=2 else []) + ways2climb(n-1,sofar+[1])

refer to this link 参考这个链接

def way_to_climb(n, path=None, val=None):
path = [] if path is None else path

val = [] if val is None else val
if n==0:
   val.append(path)
elif n==1:
    new_path = path.copy()
    new_path.append(1)
    way_to_climb(n-1, new_path, val)
    
elif n>1:
    new_path1 = path.copy()
    new_path1.append(1)
    
    way_to_climb(n-1, new_path1, val)
    
    new_path2 = path.copy()
    new_path2.append(2)
    
    way_to_climb(n-2, new_path2, val)
return val

Scroll down the link mentioned and you will find the code i pasted here.向下滚动提到的链接,您会找到我粘贴在这里的代码。

Already given by someone, this will help you out to figure out the correct answer.已经由某人给出,这将帮助您找出正确答案。

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

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