Permutations with PowerShell

A couple of blog entries ago I described how I created a mico-library of combinatorics functions using Windows PowerShell, Microsoft’s new command shell and scripting language. A solid understanding of combinatorics is absolutely essential for software test engineers, and Combinatorics has several sub-areas but the two most common are combinations and permutations. Combinations, which I discussed in the earlier blog entry, are subsets of some initial set of items, where order doesn’t matter. For example, for a set of n=3 items { "art", "bob", "cal" }, for a subset size of k=2 there are a total of 3 combination elements:
{ "art", "bob" }
{ "art", "cal" }
{ "bob", "cal" }
Permutations are a cousin to combinations. Permutations are essentially a rearrangement of a set of items. For example, for the initial set of names with n=3 listed above, there are a total of 6 permutations:
{ "art", "bob", "cal" }
{ "art", "cal", "bob" }
{ "bob", "art", "cal" }
{ "bob", "cal", "art" }
{ "cal", "art", "bob" }
{ "cal", "bob", "art" }
Being able to programmatically manipulate combinations and permutations is a key part of software test automation. Efficiently generating combinatoric elements and indirectly illustrates what I call the SAPES principle of test automation: compared to manual testing automation has superior Speed, Accuracy, Precision, Efficiency, and promotes Skill-building. Anyway, I decided to try and expanded my PowerShell micro-library to include a set of permutation methods. If you look at the screenshot below you’ll see I did just that.
The source code for the main method of my demonstration script is:
# permutationsDemo.ps1
# string permutations with PowerShell
function main
  write-host "`nbegin permutations with PowerShell demo`n"
  $data = ("ant","bat","cow","dog")
  $n = $data.length
  write-host "Initial data is: ( $data )`n"
  constructPermutation $n $data
  write-host (permutationToString)
  $total = Factorial $n
  write-host "`nThere are $total total permutation elements `n"
  for ($i = 0; $i -le $total-1; $i++) {
    if ($i -lt 10 ) { write-host " " -nonewline }
    write-host "[$i] " -nonewline
    write-host (permutationToString)
  makePermutation 12
  write-host "`nThe [12] element computed directly is: "
  write-host (permutationToString)
  write-host "`nend permutations with PowerShell demo`n"
I use a method I named constructPermutation to initialize a permutation, a method named incrementPermutation to transform the current permutation element into its lexicographical successor, a method named permutationToString to return an image of the current permutation, and a method named makePermutation which directly creates a particular permutation element. This last method uses a mathematical construct called a factoradic, a term I first published a few years ago. The net result of this whole process is that I am quite certain that PowerShell would make an excellent base technology for a lightweight software test automation certification program.
This entry was posted in Software Test Automation. Bookmark the permalink.