Module:Utils: Difference between revisions
m round(): a bugfix for x=0 |
m next_young_diagram() implemented |
||
Line 154: | Line 154: | ||
if x < 0 then return -1 end | if x < 0 then return -1 end | ||
return 0 | return 0 | ||
end | |||
-- returns the next Young diagram of the same size or nil; cannot be used with {{#invoke:}} | |||
-- modifies the input table | |||
function p.next_young_diagram(d) | |||
if #d == 0 then | |||
return nil | |||
end | |||
local i_from = nil | |||
local size = 0 | |||
for i = #d, 1, -1 do | |||
if d[i] > 1 then | |||
i_from = i | |||
break | |||
end | |||
size = size + d[i] | |||
end | |||
if i_from == nil then | |||
return nil | |||
end | |||
d[i_from] = d[i_from] - 1 | |||
size = size + 1 | |||
-- repacking the tail | |||
local max_d = d[i_from] | |||
for i = i_from + 1, #d + 1 do | |||
if size >= max_d then | |||
d[i] = max_d | |||
size = size - max_d | |||
elseif size > 0 then | |||
d[i] = size | |||
size = 0 | |||
else | |||
d[i] = nil | |||
end | |||
end | |||
return d | |||
end | end | ||
return p | return p |