Speed

Geoff Canyon gcanyon at gmail.com
Sat Aug 23 23:21:16 EDT 2014


This has several restrictions, but it generates all permutations of 10
items in about 6 seconds on my recent macbook. It uses two functions, one
to generate a permuted list of single digits, and the other to replace in
the actual items to be permuted.

The restrictions are:

1. The general permutations function P only works with up to 10 items. It
wouldn't be too hard to extend beyond that, but it would sully the very
nice (IMHO) use of single digits to store the permutations.
2. The items to be permuted cannot contain the digits 0-9. This could also
be worked around by using different characters for the permutation routine,
but making it bombproof would be harder than that.

gc


function P N
   -- returns all the permutations of the digits from 0-N
   -- only works to N = 9!!!
   if N = 0 then return 0 & cr
   put P(N - 1) into T
   repeat with i = 0 to N - 1
      put T into T2
      replace i with N in T2
      replace cr with i & cr in T2
      put T2 after R
   end repeat
   replace cr with N & cr in T
   return R & T
end P

function PLines L
   -- returns comma-delimited lines containing
   -- all the permutations of the lines in L
   -- The lines in L cannot contain any digits from 0 to
   -- the number of lines - 1
   put the number of lines of L into N
   put P(N - 1) into R
   repeat with i = 1 to N
      replace i - 1 with (line i of L) & comma in R
   end repeat
   replace comma & cr with cr in R
   return R
end PLines



More information about the use-livecode mailing list