usingMaths.com
Demonstrating and showing pupils and students one application of Mathematics.







<< PreviousNext >>

Code for Combination (Selection Without Repetition) in Python



Code for Doing Combination in Python

Writing up an algorithm to carry out the different Combination - Selection without Repetition, nCr - of a set of things in Python requires some level of imaginative thinking.

Get a writing pad and pencil:
  1. Write out all n members in the set - for Combination - at the top of the pad.
  2. Beginning with the first member, match it separately with the other members until the required selected-group size (r) is reached.
  3. When every possible Combination for this first member is exhausted, remove the current first member from the mother set.
    The immediate next member becomes the new first member in the culminating set.
  4. Take the first member in what is left of the mother set and repeat the same process from step II.

How to carry out combination

This is exactly what we will do with code to list up all possible selections without repetition in Python.

Create a new Python Class File; call it Miscellaneous.py.
Create a new Python Module File; call it Combination.py.

Type out the adjoining Python code for the combination of different options (nCr).


Why Bother About Combination

Well, isn't it obvious?
Say you are to pick only four (4) pupils from a class of six - such a small class; our little Combination algorithm solves this little problem for you by showing all your possible options / selection outcomes.









<< PreviousNext >>