Tuesday 17 March 2015

Python Program for Permutations and Combinations

Permutations and Combinations in Python:

While choosing 'r' number of items from 'n' objects there are two scenarios that are of concern. 

Firstly, we may want to choose 'r' items from 'n' objects and we do not care about the order in which they choose. 

This is called Permutation and there are two more things associated with permutation that it can be done with repetition or without repetition.

Secondly, we may want to choose 'r' items in certain order from 'n' objects.

This is called Combination and again there are two things associated with combination that it can be done with repetition and without repetition.

Here in this simple program below, I have written functions for all four scenarios above. This just takes the 'n' and 'r' as inputs from the user and prints off the results of all the four scenarios.



And, you will be able see an output like this when this code is executed.



*I am not sure, if there is a module that already deals with these functions. But atleast I hope this could be a bit useful for beginners.

No comments:

Post a Comment