I am trying to come up with a C# method that will be able to generate permutations plus variations. I think it is best explained in an example. I am spacing on the best way to accomplish this. This is how I am thinking about it in my head.
I am looking for suggestions on how to approach this, maybe pseudo code?
ABC becomes
Step 1) permutation without repetition:
ABC
ACB
BAC
BCA
CAB
CBA
Step 2) variation with |
ABC
A|BC
AB|C
ACB
A|CB
AC|B
BAC
B|AC
BA|C
BCA
B|CA
BC|A
CAB
C|AB
CA|B
CBA
C|BA
CB|A
So you end up with a final set of
A|BC
AB|C
A|CB
AC|B
B|AC
BA|C
B|CA
BC|A
C|AB
CA|B
C|BA
CB|A
Update: Just a quick implementation i wrote up, i know it’s horrible, any comments welcome.
var perms = Permuter.Permute(new Char[] {'a', 'b', 'c'}).ToList();
DisplayResult(perms);
foreach (var permutation in perms.ToList())
{
var p = permutation.ToList();
int splitPos = 1;
do
{
for (int i = 0; i < splitPos; i++)
{
Console.Write(p[i]);
}
Console.Write("|");
for (int j = splitPos; j < p.Count; j++)
{
Console.Write(p[j]);
}
Console.WriteLine("");
splitPos++;
} while (splitPos < p.Count);
}
First, generate the permutations. Lexicographic ordering is the simplest to implement; see Wikipedia and Knuth’s The Art of Computer Programming. Here’s one implementation in Python (source):
You could also use the Steinhaus–Johnson–Trotter algorithm, which is a bit faster.
To generate the variations, loop through your strings, and for each possible position within the string, insert
|in that position.