how to print combination of stairway algorithm (python)

Viewed 30

I have this code, to compute the number of ways to go up n steps by going up either one or two steps each time (this is equivalent to the Fibonacci sequence).

def find_step(n):
    if n == 0:
        return 1
    elif n < 0:
        return 0
    else:
        return find_step(n - 2) + find_step(n - 1)

How can I instead compute each of the ways, as a list showing the steps to take each time?

For example, if n is equal to 3, the result should be [1, 1, 1], [1, 2], [2, 1].

This is my attempt so far:

list_ = []
n = 4

def ways_to_climb(n, path):
    if n == 0:
        for i in path:
            print((i, end=""))
    elif n == 1:
        new_Path = [1]
        ways_to_climb(n-1, new_Path)
    elif n > 1:
        new_Path_1 = []
        new_Path_1.append(1)
        ways_to_climb(n-1, new_Path_1)
        
        new_Path_2 = []
        new_Path_2.append(2)
        ways_to_climb(n - 2, new_Path_2)
        
ways_to_climb(4, list_)

What is wrong with this code, and how do I fix it?

0 Answers
Related