Dutch national flag problem: Difference between revisions

Content added Content deleted
m (added whitespace to the task's preamble, added wording in case the image isn't viewable.)
No edit summary
Line 308: Line 308:
Original Order: WHITE, WHITE, BLUE, WHITE, BLUE, BLUE, WHITE
Original Order: WHITE, WHITE, BLUE, WHITE, BLUE, BLUE, WHITE
After Sorting: WHITE, WHITE, WHITE, WHITE, BLUE, BLUE, BLUE</pre>
After Sorting: WHITE, WHITE, WHITE, WHITE, BLUE, BLUE, BLUE</pre>

=={{header|AppleScript}}==

<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>.
-- 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)
-- A local "owner" for the potentially long 'balls' list. Speeds up references to its items and properties.
script o
property colours : {"red", "white", "blue"}
-- 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
-- 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
end repeat
log o's balls -- Log the pre-sort order.
-- Custom comparer for the sort. Decides whether or not ball 'a' should go after ball 'b'.
script redsThenWhitesThenBlues
on isGreater(a, b)
return ((a is not equal to b) and ((a is "blue") or (b is "red")))
end isGreater
end script
-- 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 o's balls
end DutchNationalFlagProblem

DutchNationalFlagProblem(100)</lang>

{{output}}
<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*)

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>


=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==