Python Challenge - Stairmaster

This community-built FAQ covers the “Stairmaster” code challenge in Python. You can find that challenge here, or pick any challenge you like from our list.

Top Discussions on the Python challenge Stairmaster

There are currently no frequently asked questions or top answers associated with this challenge – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this challenge. Ask a question or post a solution by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this challenge, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

This was a tricky one!

Essentially, you want to add the number of ways for stairmaster( n -1) , stairmaster(n-2), and stairmaster(n-3), to get the value of stairmaster(n). Since it’s really easy to calculate stairmaster() for n = 1, 2, or 3, we can just include that at the beginning of the function to give us a starting point for bigger numbers.

Then we just run a for loop for 4 to n and on each iteration:

  1. We set d = sum of the three previous values of a, b, and c
  2. We update the values of a, b, c so that they are the values needed for the next value of i.

We are then counting up to stairmaster (n) eventually. Once i reaches n, d is the sum of the 3 previous values of d, which is the number of permutations of 1, 2, 3 that equal n number of stairs, so we terminate the loop and return d as our answer.

def stairmaster(n): a = 1 b = 2 c = 4 d = 0 # fourth variable (placehold for when n > 3) if (n == 0 or n == 1 or n == 2): return n if (n == 3): return c for i in range(4,n+1): # starting from 4 because we already know the solution for, 1, 2, or 3 stairs d = c + b + a # already counted for 3 stairs a = b b = c c = d return d print(stairmaster(10))

Solved this one using recursion. Codebyte option did not work so here is just the raw code:

def stairmaster(n):
  if n == 0 or  n < 0:
    return 0
  elif n == 1:
    return 1
  elif n == 2:
    return 2
  elif n == 3:
    return 4
    return  stairmaster(n - 1) + stairmaster(n - 2) + stairmaster(n - 3)


Or this:

def stairmasterDP(n):

    memo = {0:1,1:1,2:2,3:4}

    if n in memo:

        return memo[n]


        for i in range(4,n+1):

            memo[i] = memo[i-1] + memo[i-2] + memo[i-3]

    return memo[n]


This challenge is like trying to find Fibonacci numbers, but with base cases: 1, 2, 4.

I used loops instead of recursion, and I stored the relevant previous values in a list.

def stairmaster(n, k = 3):
  if (n <= 0):
    return 0;
  elif (n <= k):
    return 2 ** (n - 1)
  recursive = [2 ** i for i in range(k)]
  for i in range(k + 1, n + 1):
    current = sum(recursive)
  return current

A list probably wasn’t the best data structure to use for this. Maybe a deque would be better.

Got an easier way:

def stairmaster(n):

import itertools as it



for num in range(1,n+1):

for way in it.product(staircase, repeat=num):

  if sum(way)==n:


return ways


CANT TEST CODE DUE TO ERROR. but im confident this is correct

I tried to solve this challenge by using itertools too. I think the Problem is, that the calculations take a lot of time. The last result I was able to calculate was with n = 17 (or something like that). Wether this way works or not, understanding the pattern (took me a while for that) ist the much more efficient way.

def stairmaster(n): if n == 3: return 4 elif (n <= 0 or n == 1 or n == 2): return n else: count = 0 for i in range(1,4): count += stairmaster(n-i) return count print(stairmaster(10))

recursion good

def stairmaster(n): if n == 0 or n == 1 or n == 2: return n if n == 3: return 4 return stairmaster(n-1) + stairmaster(n-2) + stairmaster(n-3) print(stairmaster(20))
def stairmaster(n): if n == 3: return 4 if n == 0 or n == 1 or n == 2: return n return stairmaster(n-1) + stairmaster(n-2) + stairmaster(n-3) print(stairmaster(4))