RosettaCodeData/Task/Permutations/AppleScript/permutations-2.applescript

43 lines
1.7 KiB
AppleScript
Raw Permalink Normal View History

2017-09-23 10:01:46 +02:00
to DoPermutations(aList, n)
2020-02-17 23:21:07 -08:00
--> Heaps's algorithm (Permutation by interchanging pairs)
2017-09-23 10:01:46 +02:00
if n = 1 then
2020-02-17 23:21:07 -08:00
tell (a reference to PermList) to copy aList to its end
2017-09-23 10:01:46 +02:00
-- or: copy aList as text (for concatenated results)
else
repeat with i from 1 to n
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] -- swaps items i and n of aList
else
tell aList to set [item 1, item n] to [item n, item 1] -- swaps items 1 and n of aList
end if
end repeat
end if
2020-02-17 23:21:07 -08:00
return (a reference to PermList) as list
2017-09-23 10:01:46 +02:00
end DoPermutations
--> Example 1 (list of words)
2020-02-17 23:21:07 -08:00
set [SourceList, PermList] to [{"Good", "Johnny", "Be"}, {}]
2017-09-23 10:01:46 +02:00
DoPermutations(SourceList, SourceList's length)
2020-02-17 23:21:07 -08:00
--> result (value of PermList)
2017-09-23 10:01:46 +02:00
{{"Good", "Johnny", "Be"}, {"Johnny", "Good", "Be"}, {"Be", "Good", "Johnny"}, ¬
{"Good", "Be", "Johnny"}, {"Johnny", "Be", "Good"}, {"Be", "Johnny", "Good"}}
--> Example 2 (characters with concatenated results)
2020-02-17 23:21:07 -08:00
set [SourceList, PermList] to [{"X", "Y", "Z"}, {}]
2017-09-23 10:01:46 +02:00
DoPermutations(SourceList, SourceList's length)
2020-02-17 23:21:07 -08:00
--> result (value of PermList)
2017-09-23 10:01:46 +02:00
{"XYZ", "YXZ", "ZXY", "XZY", "YZX", "ZYX"}
--> Example 3 (Integers)
set [SourceList, Permlist] to [{1, 2, 3}, {}]
DoPermutations(SourceList, SourceList's length)
--> result (value of Permlist)
{{1, 2, 3}, {2, 1, 3}, {3, 1, 2}, {1, 3, 2}, {2, 3, 1}, {3, 2, 1}}
--> Example 4 (Integers with concatenated results)
set [SourceList, Permlist] to [{1, 2, 3}, {}]
DoPermutations(SourceList, SourceList's length)
--> result (value of Permlist)
{"123", "213", "312", "132", "231", "321"}