Program for number of stopping station problems using Python

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 for number of stopping station problems using Python

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

Program for number of stopping station problems using Python 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.

Author

  • Barry Allen

    A Full Stack Developer with 10+ years of experience in different domain including SAP, Blockchain, AI and Web Development.

    View all posts

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.