Best shuffle: Difference between revisions

m (→‎{{header|Ruby}}: Fix link: Perl 6 --> Raku)
Line 2,212:
up pu 0
a a 1</pre>
 
 
=={{header|Lua}}==
<lang lua>math.randomseed(os.time())
 
local function shuffle(t)
for i = #t, 2, -1 do
local j = math.random(i)
t[i], t[j] = t[j], t[i]
end
end
 
local function bestshuffle(s, r)
local order, shufl, count = {}, {}, 0
for ch in s:gmatch(".") do order[#order+1], shufl[#shufl+1] = ch, ch end
if r then shuffle(shufl) end
for i = 1, #shufl do
for j = 1, #shufl do
if i ~= j and shufl[i] ~= order[j] and shufl[j] ~= order[i] then
shufl[i], shufl[j] = shufl[j], shufl[i]
end
end
end
for i = 1, #shufl do
if shufl[i] == order[i] then
count = count + 1
end
end
return table.concat(shufl), count
end
 
local words = { "abracadabra", "seesaw", "elk", "grrrrrr", "up", "a" }
 
local function test(r)
print(r and "RANDOM:" or "DETERMINISTIC:")
for _, word in ipairs(words) do
local shufl, count = bestshuffle(word, r)
print(string.format("%s -> %s (%d)", word, shufl, count))
end
print()
end
 
test(true)
test(false)</lang>
{{out}}
<pre>RANDOM:
abracadabra -> caadabarrab (0)
seesaw -> essewa (0)
elk -> lke (0)
grrrrrr -> rrrgrrr (5)
up -> pu (0)
a -> a (1)
 
DETERMINISTIC:
abracadabra -> caadrbabaar (0)
seesaw -> ewaess (0)
elk -> kel (0)
grrrrrr -> rgrrrrr (5)
up -> pu (0)
a -> a (1)</pre>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
Anonymous user