Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
41
Task/Best-shuffle/Lua/best-shuffle.lua
Normal file
41
Task/Best-shuffle/Lua/best-shuffle.lua
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
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)
|
||||
Loading…
Add table
Add a link
Reference in a new issue