Permutations: Difference between revisions

Line 573:
DoPermutations(aList, n - 1)
if n mod 2 = 0 then -- n is even
tell aList to set [item i, item n] to [item n, item i] -- swapswaps items i and n of aList
else
tell aList to set [item 1, item n] to [item n, item 1] -- swapswaps items 1 and n of aList
end if
set i to i + 1
end repeat
end if
return (a reference to Permlist) as list
end DoPermutations
 
Line 585 ⟶ 586:
set [SourceList, Permlist] to [{"Good", "Johnny", "Be"}, {}]
DoPermutations(SourceList, SourceList's length)
Permlist
--> result
{{"Good", "Johnny", "Be"}, {"Johnny", "Good", "Be"}, {"Be", "Good", "Johnny"},¬
Line 593:
set [SourceList, Permlist] to [{"X", "Y", "Z"}, {}]
DoPermutations(SourceList, SourceList's length)
Permlist
--> result
{"XYZ", "YXZ", "ZXY", "XZY", "YZX", "ZYX"}
Line 600 ⟶ 599:
set [SourceList, Permlist] to [{1, 2, 3}, {}]
DoPermutations(SourceList, SourceList's length)
Permlist
--> result
{"123", "213", "312", "132", "231", "321"}</lang>