Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
17
Task/Permutations/Lua/permutations-1.lua
Normal file
17
Task/Permutations/Lua/permutations-1.lua
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
local function permutation(a, n, cb)
|
||||
if n == 0 then
|
||||
cb(a)
|
||||
else
|
||||
for i = 1, n do
|
||||
a[i], a[n] = a[n], a[i]
|
||||
permutation(a, n - 1, cb)
|
||||
a[i], a[n] = a[n], a[i]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
--Usage
|
||||
local function callback(a)
|
||||
print('{'..table.concat(a, ', ')..'}')
|
||||
end
|
||||
permutation({1,2,3}, 3, callback)
|
||||
31
Task/Permutations/Lua/permutations-2.lua
Normal file
31
Task/Permutations/Lua/permutations-2.lua
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
-- Iterative version
|
||||
function ipermutations(a,b)
|
||||
if a==0 then return end
|
||||
local taken = {} local slots = {}
|
||||
for i=1,a do slots[i]=0 end
|
||||
for i=1,b do taken[i]=false end
|
||||
local index = 1
|
||||
while index > 0 do repeat
|
||||
repeat slots[index] = slots[index] + 1
|
||||
until slots[index] > b or not taken[slots[index]]
|
||||
if slots[index] > b then
|
||||
slots[index] = 0
|
||||
index = index - 1
|
||||
if index > 0 then
|
||||
taken[slots[index]] = false
|
||||
end
|
||||
break
|
||||
else
|
||||
taken[slots[index]] = true
|
||||
end
|
||||
if index == a then
|
||||
for i=1,a do io.write(slots[i]) io.write(" ") end
|
||||
io.write("\n")
|
||||
taken[slots[index]] = false
|
||||
break
|
||||
end
|
||||
index = index + 1
|
||||
until true end
|
||||
end
|
||||
|
||||
ipermutations(3, 3)
|
||||
41
Task/Permutations/Lua/permutations-3.lua
Normal file
41
Task/Permutations/Lua/permutations-3.lua
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
#!/usr/bin/env luajit
|
||||
-- Iterative version
|
||||
local function ipermgen(a,b)
|
||||
if a==0 then return end
|
||||
local taken = {} local slots = {}
|
||||
for i=1,a do slots[i]=0 end
|
||||
for i=1,b do taken[i]=false end
|
||||
local index = 1
|
||||
while index > 0 do repeat
|
||||
repeat slots[index] = slots[index] + 1
|
||||
until slots[index] > b or not taken[slots[index]]
|
||||
if slots[index] > b then
|
||||
slots[index] = 0
|
||||
index = index - 1
|
||||
if index > 0 then
|
||||
taken[slots[index]] = false
|
||||
end
|
||||
break
|
||||
else
|
||||
taken[slots[index]] = true
|
||||
end
|
||||
if index == a then
|
||||
coroutine.yield(slots)
|
||||
taken[slots[index]] = false
|
||||
break
|
||||
end
|
||||
index = index + 1
|
||||
until true end
|
||||
end
|
||||
local function iperm(a)
|
||||
local co=coroutine.create(function() ipermgen(a,a) end)
|
||||
return function()
|
||||
local code,res=coroutine.resume(co)
|
||||
return res
|
||||
end
|
||||
end
|
||||
|
||||
local a=arg[1] and tonumber(arg[1]) or 3
|
||||
for p in iperm(a) do
|
||||
print(table.concat(p, " "))
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue