Quantcast
Channel: Planet Python
Viewing all articles
Browse latest Browse all 22871

IslandT: Python Example — Write a Python program to find the permutation of an event

$
0
0

In this example let us create a Python program that will find the permutation of an event. A permutation event is very confusing to me and I am still learning it thus this article is not about explaining what is actually permutation but instead demonstrating to you all how to create the Python program which calculates the permutation event.

According to the rule of permutation if you want to know how many distinct orders you can arrange a group of n elements from a set of N distinctly different elements you will apply this formula:

N!/(N-n)!

For example, if you want to find out how many distinct orders can you arrange 20 different job seekers for three job positions, A, B, and C then you can apply the permutation formula as follows:

N = 20
n = 3

thus 20!/(20-3)! = 20 * 19 * 18 = 6840.

The Python program below will call its own function to calculate the factorial part as well as call the factorial function within the main function.

def factorial(n):

    if n > 1:
        return n * factorial(n-1)
    else:
        return 1

def arrange(N, n):

    top = factorial(N)
    bottom = factorial(N-n)
    return int(top/bottom) # eliminated the decimal places

print(arrange(20, 3)) # 6840

Another example: How many distinct orders can you arrange 5 chairs from a set of 5?

In this example, n equals N thus (N-n)! = 0! and you will get 5!/0! which is equal to 5 * 4 * 3 * 2 * 1 / 1 = 120. Let see whether the answer is tally with the permutation program :

print(arrange(5, 5)) # 120

The outcome of the permutation program has matched the result that I calculated previously!


Viewing all articles
Browse latest Browse all 22871

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>