ProgrammingHardTCS NQT · Accenture

What is printed?

What is printed?
def climb(n, steps):
    dp = [0] * (n + 1)
    dp[0] = 1
    for i in range(1, n + 1):
        for s in steps:
            if i - s >= 0:
                dp[i] += dp[i - s]
    return dp[n]
print(climb(5, [1, 3, 4]), climb(4, [2, 3]))
  1. A. 5 1
  2. B. 6 2
  3. C. 6 1
  4. D. 4 1

Correct answer: C. 6 1

dp[i] adds up the ways for each allowed step, counting different orders as different. Steps {1, 3, 4} give 6 ways for n = 5, and steps {2, 3} give exactly one way for n = 4, namely 2 + 2.

More Python Programming questions

Exams that ask this topic