Staircase challenge - Python Your task is to print all unique ways to climb a staircase of length n where, from each step, you can take steps of size x where x is an integer value from an array
  For example: if you want to climb a staircase of length 3 and you can move [1, 2] steps from each stair, the output should be

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

  
  Note the order is important and hence [1, 2] is different
  to [2, 1].


Is this the fastest way to solve this problem (without using built in libraries such as itertools)?

def valid_moves(position, target, legal, path, permutations):
    if position &gt; target:
        return
    if position == target:
        permutations.append(path)
        return 
    for move in legal:
        valid_moves(position + move, target, legal, path + [move], permutations)
    return permutations


start = 0
length_staircase = 3
legal_moves = [1, 2]

print(*valid_moves(start, length_staircase, legal_moves, [], []), sep="\n")
I have a few suggestions for you:


You should get in the habit on wrapping all code that isn't in a function in a main guard, to ensure that that code only runs if that file is running, and to protect it from import mishaps
Running your code with pylint, a few warnings that popped up were:


Returns: All return statements should return something or none of them should return anything. Since you are returning permutations, the other return's should just return None.
Module Docstring: Your function should contain a docstring describing what the function does, even if it's very obvious. These will help any documentation.
Constant variable names: Since the three variables in the main guard do not change, they should be in all UPPERCASE to make it clear that they are constants.



Refactored code:

def valid_moves(position, target, legal, path, permutations):
    """ Returns unique ways to climb a staircase with passed length `target` """
    if position &gt; target:
        return None
    if position == target:
        permutations.append(path)
        return None
    for move in legal:
        valid_moves(position + move, target, legal, path + [move], permutations)
    return permutations

if __name__ == '__main__':
    START = 0
    STAIRCASE_LENGTH = 3
    LEGAL_MOVES = [1, 2]

    print(*valid_moves(START, STAIRCASE_LENGTH, LEGAL_MOVES, [], []), sep="\n")