permuting a string (was Re: Speed)

Geoff Canyon gcanyon at gmail.com
Sat Aug 30 00:48:17 EDT 2014


On Wed, Aug 27, 2014 at 9:33 AM, Beat Cornaz <B.Cornaz at gmx.net> wrote:

> So, getting rid of the duplicates inside the script is quite important. I
> still don't see yet how I can do that in Geoff's script (I will look into
> that again, as soon as I can find a little time). If we can get that to
> work, I think we'll have a winner :-)
>

Okay, I have this figured out but not debugged. It's not amazingly pretty,
but it handles duplicates (with restrictions) and is fast. What I did is:

Used Alex's code to generate a list of the permutations of all the
characters that were duplicates.
Substituted in unique characters for each instance of the duplicates.
Ran my permutation code on the rest of the characters, with the addition of
the duplicates-permutation-strings as the base case.

So for example, to get all the permutations of aaaabbbbijkl, it would:

Use Alex's code to get the 70 permutations of aaaabbbb.
Convert those strings to use abcd for the four a's and efgh for the 4 b's.
Use those 70 strings as the result of P2 when the argument for length is 7

It generates the 831,600 unique permutations of a 12 character string where
there are two sets of four duplicate characters in a little under half a
second on my computer. This compares to my original script, which generates
the 39,916,800 permutations of an 11 character string in about 6 seconds,
and dies calculating the 479,001,600 permutations of a 12 character string.

I'll see if I can debug and post actual tomorrow.

gc



More information about the use-livecode mailing list