Speed
Peter M. Brigham
pmbrig at gmail.com
Tue Aug 26 13:57:54 EDT 2014
A followup on how to handle duplicate characters in the permuting algorithm. The following seems to work, not sure how it will scale. tString can contain any characters -- duplicates, digits, spaces, whatever.
function permute tString
-- returns all the permutations in the string tString
put (the length of tString) into N
repeat with c = 1 to N
put char c of tString into charAtLoc[c]
end repeat
subtract 1 from N
put P2(N,49) into permList
repeat with c = 1 to N+1
replace c with charAtLoc[c] in permList
end repeat
return permList
end permute
function P2 N,B
-- N is the depth to permute
-- B is the ASCII value to start from
-- so P2(1,49) returns 21 cr 12
-- P2(2,53) returns 675 cr 765 cr 756 cr 576 cr 657 cr 567
if N = 0 then return numToChar(B) & cr
put P2(N - 1,B) into T
add B to N
repeat with i = B to N - 1
put T into T2
replace numToChar(i) with numToChar(N) in T2
replace cr with numToChar(i) & cr in T2
put T2 after R
end repeat
replace cr with numToChar(N) & cr in T
return R & T
end P2
-- Peter
Peter M. Brigham
pmbrig at gmail.com
http://home.comcast.net/~pmbrig
More information about the use-livecode
mailing list