Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1,41 @@
|
|||
_JT={}
|
||||
function JT(dim)
|
||||
local n={ values={}, positions={}, directions={}, sign=1 }
|
||||
setmetatable(n,{__index=_JT})
|
||||
for i=1,dim do
|
||||
n.values[i]=i
|
||||
n.positions[i]=i
|
||||
n.directions[i]=-1
|
||||
end
|
||||
return n
|
||||
end
|
||||
|
||||
function _JT:largestMobile()
|
||||
for i=#self.values,1,-1 do
|
||||
local loc=self.positions[i]+self.directions[i]
|
||||
if loc >= 1 and loc <= #self.values and self.values[loc] < i then
|
||||
return i
|
||||
end
|
||||
end
|
||||
return 0
|
||||
end
|
||||
|
||||
function _JT:next()
|
||||
local r=self:largestMobile()
|
||||
if r==0 then return false end
|
||||
local rloc=self.positions[r]
|
||||
local lloc=rloc+self.directions[r]
|
||||
local l=self.values[lloc]
|
||||
self.values[lloc],self.values[rloc] = self.values[rloc],self.values[lloc]
|
||||
self.positions[l],self.positions[r] = self.positions[r],self.positions[l]
|
||||
self.sign=-self.sign
|
||||
for i=r+1,#self.directions do self.directions[i]=-self.directions[i] end
|
||||
return true
|
||||
end
|
||||
|
||||
-- test
|
||||
|
||||
perm=JT(4)
|
||||
repeat
|
||||
print(unpack(perm.values))
|
||||
until not perm:next()
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
local wrap, yield = coroutine.wrap, coroutine.yield
|
||||
local function perm(n)
|
||||
local r = {}
|
||||
for i=1,n do r[i]=i end
|
||||
local sign = 1
|
||||
return wrap(function()
|
||||
local function swap(m)
|
||||
if m==0 then
|
||||
sign = -sign, yield(sign,r)
|
||||
else
|
||||
for i=m,1,-1 do
|
||||
r[i],r[m]=r[m],r[i]
|
||||
swap(m-1)
|
||||
r[i],r[m]=r[m],r[i]
|
||||
end
|
||||
end
|
||||
end
|
||||
swap(n)
|
||||
end)
|
||||
end
|
||||
for sign,r in perm(3) do print(sign,table.unpack(r))end
|
||||
Loading…
Add table
Add a link
Reference in a new issue