Dutch national flag problem: Difference between revisions

Content added Content deleted
(Add Common Lisp (based on Clojure's structure))
m (→‎{{header|AppleScript}}: New sort handler URL, tidy-up.)
Line 492: Line 492:


=={{header|AppleScript}}==
=={{header|AppleScript}}==
<syntaxhighlight lang="applescript">use AppleScript version "2.3.1" -- OS X 10.9 (Mavericks) or later.

use sorter : script ¬
<syntaxhighlight lang="applescript">use AppleScript version "2.3.1" -- OS X 10.9 (Mavericks) or later — for these 'use' commands!
-- This script uses a customisable AppleScript sort available at <https://macscripter.net/viewtopic.php?pid=194430#p194430>.
"Custom Iterative Ternary Merge Sort" --<www.macscripter.net/t/timsort-and-nigsort/71383/3>
-- It's assumed that scripters will know how and where to install it as a library.
use sorter : script "Custom Iterative Ternary Merge Sort"


on DutchNationalFlagProblem(numberOfBalls)
on DutchNationalFlagProblem(numberOfBalls)
-- A local "owner" for the potentially long 'balls' list. Speeds up references to its items and properties.
script o
script o
property colours : {"red", "white", "blue"}
property colours : {"red", "white", "blue"}
property balls : {}
-- Initialise the balls list with at least one instance of each colour — but not in Dutch flag order!
property balls : reverse of my colours
end script
end script
repeat numberOfBalls times
-- Randomly fill the list from the three colours to the required number of balls (min = 3).
-- The task description doesn't say if there should be equal numbers of each colour, but it makes no difference to the solution.
repeat numberOfBalls - 3 times
set end of o's balls to some item of o's colours
set end of o's balls to some item of o's colours
end repeat
end repeat
log o's balls -- Log the pre-sort order.
log o's balls
-- Sort the balls using a custom comparison handler.
-- Custom comparer for the sort. Decides whether or not ball 'a' should go after ball 'b'.
script redsThenWhitesThenBlues
script redWhiteBlue
on isGreater(a, b)
on isGreater(a, b)
return ((a is not equal to b) and ((a is "blue") or (b is "red")))
return ((a b) and ((a is "blue") or (b is "red")))
end isGreater
end isGreater
end script
end script
tell sorter to sort(o's balls, 1, -1, {comparer:redWhiteBlue})
-- Sort items 1 thru -1 of the balls (ie. the whole list) using the above comparer.
tell sorter to sort(o's balls, 1, -1, {comparer:redsThenWhitesThenBlues})
-- Return the sorted list.
-- Return the sorted list.
Line 530: Line 523:
{{output}}
{{output}}
<pre>Log:
<pre>Log:
(*blue, white, red, red, red, white, red, white, white, red, blue, red, blue, red, red, white, white, blue, blue, blue, blue, white, blue, blue, white, white, blue, blue, red, white, red, red, red, red, red, red, red, red, blue, white, white, blue, blue, blue, blue, blue, white, red, white, red, red, blue, white, red, blue, red, blue, blue, red, blue, blue, white, blue, blue, blue, red, red, blue, red, red, white, blue, blue, white, white, white, white, red, blue, white, white, red, red, white, red, blue, white, red, red, blue, blue, blue, white, blue, white, red, blue, blue, white, red*)
(*blue, blue, blue, white, blue, white, white, white, red, blue, red, red, white, white, blue, white, blue, white, red, blue, white, white, blue, white, red, blue, white, white, white, blue, blue, red, blue, red, red, blue, white, white, red, white, red, red, red, blue, red, blue, red, blue, red, white, blue, white, red, white, red, white, white, blue, red, blue, blue, red, white, blue, red, white, blue, white, red, blue, white, white, white, blue, red, white, white, white, white, blue, red, red, white, red, red, red, white, white, red, blue, white, red, red, red, red, red, white, red, red, white*)


Result:
Result:
{"red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue"}</pre>
{"red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue"}</pre>


=={{header|Applesoft BASIC}}==
=={{header|Applesoft BASIC}}==