Table of Contents
Introduction
The task is to find the number of ways we can stop the train at K stations out of N stations between S1 and S2 such that the stopping stations are non-consecutive.
Program
def find( s, ts): #select s non-consecutive station from ts number of stations " temp, temp1 = 1, 1 pos = s while s != 1: temp1 *= s s -= 1 i = ts - pos + 1 while i != (ts-2 * pos + 1): temp *= i i -= 1 if (ts - pos + 1) >= pos: return int(temp/temp1) else: return -1 ip_stopping_stations = find(2, 14) if ip_stopping_stations != -1: print("Number of ways: ",ip_stopping_stations) else: print("Selection not possible!")
Output
Explanation
In the above code, we have used mathematical expression of combination (n-p+1)Cp where n is the number of station and p is the number of stopping stations.
0 Comments