RosettaCodeData/Task/Dutch-national-flag-problem/Icon/dutch-national-flag-problem.icon
Ingy döt Net 776bba907c Sync
2013-10-27 22:24:23 +00:00

22 lines
688 B
Text

procedure main(a)
n := integer(!a) | 20
every (nr|nw|nb) := ?n-1
sIn := repl("r",nw)||repl("w",nb)||repl("b",nr)
write(sRand := bestShuffle(sIn))
write(sOut := map(csort(map(sRand,"rwb","123")),"123","rwb"))
if sIn ~== sOut then write("Eh? Not in correct order!")
end
procedure bestShuffle(s) # (Taken from the Best Shuffle task)
t := s
every !t :=: ?t # Uncommented to get a random best shuffling
every i := 1 to *t do
every j := (1 to i-1) | (i+1 to *t) do
if (t[i] ~== s[j]) & (s[i] ~== t[j]) then break t[i] :=: t[j]
return t
end
procedure csort(w)
every (s := "") ||:= (find(c := !cset(w),w),c)
return s
end