- 0
Notifications Mark All Read
- Login
- Get Prime
Python Program for Staircase Problem
Staircase Problem
TCS CodeVita is a coding competition organized by TCS every year in search for some of the best coders all over the world. This year it is the ninth season of this competition known as TCS CodeVita 2020 season 9. Before the actual competition TCS CodeVita gives a set of sample question, Staircase Problem is one of the sample question of this year TCS CodeVita Season 9. Here we have solved this problem in Python.
Problem Description
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time.
- Count the number of ways, the person can reach the top.
Python Code
n = int(input("Enter number of Stairs:")) def calc(x): if x <= 1: return x return calc(x - 1) + calc(x - 2) def count(n): return calc(n + 1) print("Number of ways:", count(n))
Output 5 8
Staircase Problem in Other Coding Languages
C
C++
To find the solution of Staircase problem in C++ Programming language click on the button below:
Java
To find the solution of Staircase problem in Java Programming language click on the button below:
fib = [0,1]
for i in range(50):
sum = fib[i]+fib[i+1]
fib.append(sum)
n=int(input(“number of staircase:”))
print(fib[n+1])
import math
n = int(input(‘Enter a number of stairs: ‘))
combi = 0
for i in range(0,n+1,2):
twos = i//2 # Number of 2 steps
ones = n – i # Number of 1 steps
counter = twos + ones # Total number of footsteps
temp = math.factorial(counter)//(math.factorial(ones)*math.factorial(twos)) # All combinations
combi += temp
print(‘Total combinations: ‘,combi)
This is my code for the staircase problem with the concept of permutations and combinations
Please do check if this code works
# Using DP (use Dictionary as a memorization tool)
num = int(input())
def remain_stair(n,memo={}):
#print(memo)
if n<=2:
memo[n] = n
return n
elif n in memo:
return memo[n]
memo[n] = remain_stair(n-1,memo) + remain_stair(n-2,memo)
#print(str(n)+" = "+str(memo[n]))
return memo[n]
print(remain_stair(num))
n = int(input(“Enter number of Stairs:”))
dp = [1,1]
for _ in range(1,n):
dp.append(dp[-2] + dp[-1])
print(dp[-1])