permuting a string (was Re: Speed)

Beat Cornaz B.Cornaz at gmx.net
Wed Aug 27 09:33:22 EDT 2014


Thanks Peter,
your script works, but is in the same speed region as my original script, with the added disadvantage that it can't go beyond 9 chars.

As for the duplicate elements : I did the same before - make all the possible permutations and then delete the duplicate ones. 
But as Geoff rightly points out and I had discovered as well, that doesn't scale well. If we manage to delete duplicates in an earlier stage, many permutations down the line need not to be made, which can make it much faster. 

I do the deletions of duplicates in my script now as early as possible, which all of a sudden makes it faster than Geoff's script if I have many duplicates in the Input.

My script             10 input elements no duplicates                                       -  4407 millisecs
Geoff's script       idem                                                                                      -   474   millisecs
My script             10 input elements duplicates (0001112223)                  -   56 milli
Geoff's script       idem   with deletion of duplicates at end                        -    534 milli   (the normal script + the time for the deletions at the end)

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 :-)


my script :

function BC4_PermMech4_Duplicates_Lines ToPermutate
   set cursor to busy
 
   put empty into TempPerms5
   put the number of lines of ToPermutate into NumElements
   
   --  *** First calc the upper half. 
   
   put line 1 of ToPermutate & comma  &  line 2 of ToPermutate  & comma into TempPerms1
   put line 3 to -1 of  ToPermutate into tInput3
   put 3 into Index
   
   repeat for each line newElement in tInput3
      set the cursor to busy
      repeat for each line tLine in TempPerms1
         repeat with y = Index down to 1         
            put tLine into Temp 
            put newElement & comma before item y of temp
            put Temp & cr after TempPerms5
         end repeat
      end repeat
      
     put BC2_RemoveDuplicate_LooseOrder_Lines (TempPerms5)  into TempPerms1      -- #### HERE IS THE DELETION HANDLER

      put empty into TempPerms5
      add 1 to Index
      
   end repeat
   
   
   --  *** Now the second half
   --  *** Copy first half as second half and replace in  the second half 
   
   put TempPerms1 into TempPerms2
   
   replace 1 with "z" in TempPerms2
   replace 2 with 1 in TempPerms2
   replace "z" with 2 in TempPerms2
   
   put TempPerms2  after TempPerms1
   delete char -1 of TempPerms1
   
   choose browse tool
   
   Return TempPerms1
end BC4_PermMech4_Duplicates_Lines 

---
function BC2_RemoveDuplicate_LooseOrder_Lines pData
   put ":" & cr into DD
   
   replace cr with DD in pData
   split pData by cr and ":"
   Return the keys of pData 
   
end BC2_RemoveDuplicate_LooseOrder_Lines


--------




More information about the use-livecode mailing list