Google Kickstart
Train Problem In Python
There are number intermediate stations between two places A and B. Find the number of ways in which a train can be made to stop at number of these intermediate stations so that no two stopping stations are consecutive?
Testcases1:
Input:
Tstation=10
n=5
Output:
6
Testcases2:
Input:
Tstation=15
n=6
Output=210
#In this program there is just to find no of ways i.e we have learnt already in class11 maths permutation and combination chapter ch-7.
So here just to find the combination i.e in how many ways the train can stop without consecutive station.
"""
Write a program to count the ways to stop the train without consecutive stations
Factorial means 4!=4*3*2*1=24
5=5*4*3*2*1=120
This is docs string
"""
#Let initialize the program and understand the program by dividing in small parts
#first input total number of intermediate station
#Then take input how many stations train has to stop without consecutive stations ley understand
# 1 2 3 4 5 6 7 8 9 10 11 12
#here 1 is represent first station similarily numbers represnt name of station
#Tstation is a variable to input totoal number of station
Tstation=int(input("Enter a number of total stations:"))
#n is a number of stations where train will stop
n=int(input("Enter in how many stations have to stop the train without consecutive station: "))
Noofways=0
#here if we have to stop only at 4 stations in between 12 stations,so
#first we will remove four station where train will stop.
#Now remainings are
# # # # # # # # #
#And space between these remaing stations are 9 .
#so no of ways of where train will not stop is 9 C4 ways
#because nCr=(n!)/((n-r)!*r!)
#therefore 9C4=9!/(9-4)!*4!
! this sign represent factorial of that number
#Here remaining mean if Tstation=12 and n=4 then remaining=12-4+1=9
remaining=Tstation-n+1
#now we will go to math library to get factorial function to get factorial of the numbers
from math import factorial
Noofways=(factorial(remaining))//(factorial(n)*factorial(remaining-n))
#Then use print() function to print number of ways
print(Noofways)
Let us understand by read steps given below:
No comments: