
The Dutch national flag is composed of three coloured bands in the order:

   red     (top)
   then white,   and
   lastly blue   (at the bottom). 


The problem posed by Edsger Dijkstra is:

Given a number of red, blue and white balls in random order, arrange them in the order of the colours in the Dutch national flag.
When the problem was first posed, Dijkstra then went on to successively refine a solution, minimising the number of swaps and the number of times the colour of a ball needed to determined and restricting the balls to end in an array, ...



Task
 Generate a randomized order of balls ensuring that they are not in the order of the Dutch national flag.
 Sort the balls in a way idiomatic to your language.
 Check the sorted balls are in the order of the Dutch national flag.


C.f.

 Dutch national flag problem
 Probabilistic analysis of algorithms for the Dutch national flag problem by Wei-Mei Chen. (pdf)


defmodule Dutch_national_flag do
  defp ball(:red),   do: 1
  defp ball(:white), do: 2
  defp ball(:blue),  do: 3
 
  defp random_ball, do: Enum.random([:red, :white, :blue])
 
  defp random_ball(n), do: (for _ <- 1..n, do: random_ball())
 
  defp is_dutch([]), do: true
  defp is_dutch([_]), do: true
  defp is_dutch([b,h|l]), do: ball(b) < ball(h) and is_dutch([h|l])
  defp is_dutch(_), do: false
 
  def  dutch(list), do: dutch([], [], [], list)
 
  defp dutch(r, w, b, []),              do: r ++ w ++ b
  defp dutch(r, w, b, [:red   | list]), do: dutch([:red | r],  w,  b, list)
  defp dutch(r, w, b, [:white | list]), do: dutch(r, [:white | w], b, list)
  defp dutch(r, w, b, [:blue  | list]), do: dutch(r, w,  [:blue | b], list)
 
  def problem(n \\ 10) do
    list = random_ball(n)
    if is_dutch(list) do
      IO.puts "The random sequence #{inspect list} is already in the order of the Dutch flag!"
    else
      IO.puts "The starting random sequence is #{inspect list};"
      IO.puts "The ordered sequence is #{inspect dutch(list)}."
    end
  end
end
 
Dutch_national_flag.problem