24 lines
679 B
Text
24 lines
679 B
Text
$define NUM_BALLS = 9
|
|
|
|
local colors = {"Red", "White", "Blue"}
|
|
local color_map = {Red = 1, White = 2, Blue = 3}
|
|
|
|
local function is_sorted(balls, color_cmp)
|
|
if #balls <= 1 then return true end
|
|
for i = 2, #balls do
|
|
if !color_cmp(balls[i], balls[i - 1]) then return false end
|
|
end
|
|
return true
|
|
end
|
|
|
|
local balls = {}
|
|
for i = 1, NUM_BALLS do balls[i] = colors[1] end
|
|
local color_cmp = |c1, c2| -> color_map[c1] < color_map[c2]
|
|
|
|
repeat
|
|
for i = 1, NUM_BALLS do balls[i] = colors[math.random(3)] end
|
|
until !is_sorted(balls, color_cmp)
|
|
|
|
print($"Before sorting : {balls:concat(", ")}")
|
|
table.sort(balls, color_cmp)
|
|
print($"After sorting : {balls:concat(", ")}")
|