Combinations n Permutations in QA

Combinations n Permutations are very useful in software testing.
It help to find possible test-data / testcases. This session will provide details about

1. Some basics about Combinations
2. Logic to find possible combinations for taking k numbers of items from n number items at a time.

1. Some basics about Combinations.
You might be come across with below example in old school days.
We have set { "ant", "bug", "cat", "dog", "pig" } and If we want to take 3 elements at a time then we would have possible combinations as
{ "ant", "bug", "cat" }
{ "ant", "bug", "dog" }
{ "ant", "bug", "pig" }
{ "ant", "cat", "dog" }
{ "ant", "cat", "pig" }
{ "ant", "dog", "pig" }
{ "bug", "cat", "dog" }
{ "bug", "cat", "pig" }
{ "bug", "dog", "pig" }
{ "cat", "dog", "pig" }

So for n=5 and k=3 in above we would have 10 possible combinations.
How we can count ?
We have formula :

Total number of combinations = n * (n-1) * (n-2) * ....* (n - k +1) \ 1 * 2 * ...* k

So, n= 5 and k= 3
Total number of combinations = 5 * 4 * 3 \ 1 * 2 * 3 = 10
We will use this formula to implement C# function Choose(n,k) to find total number of possible combinations in coming session.


2. Logic to find possible combinations for taking k numbers of items from n number items at a time.
The basic logic is to get all possible combinations item index.
So, If we have data[5] = { "ant", "bug", "cat", "dog", "pig" }
{ "ant", "bug", "cat" } = { data[0] , data[1] , data[2] }
and so we need to get { 0 , 1 , 2 }
{ "ant", "bug", "dog" } = { data[0] , data[1] , data[3] }
and so we need to get { 0 , 1 , 3 }

And for others as respectively we need to get
{ 0 , 1 , 4 }
{ 0 , 2 , 3 }
{ 0 , 2 , 4 }
{ 0 , 3 , 4 }
{ 1 , 2 , 3 }
{ 1 , 2 , 4 }
{ 1 , 3 , 4 }
{ 2 , 3 , 4 }

To find combinations we will use lexicographical algorithm.
In this algorithm, we find successor. Successor is the next possible combination.
Say for { 0 , 1 , 2 } we have successor { 0 , 1 , 3 }
The core logic to get successor is first to find right most element that must be incremented then we increment all right most elements for that we will create Combination Class and implement two constructor to set base data, n and k of the set as per requirement and then execute Successor Method and will store all possible combinations of object array and display as well.

Inspired by article of Dr. James McCaffrey on String Permutation.

Comments

Popular posts from this blog

Testing Shifts with agile automation #RPA #ML

Bots and What Not !!

Need > Want

Automate your everyday tedious tasks and free up time for higher-value work (with Microsoft free RPA solution)

Respect is not earned, It is given ⚘ 🙏