Skip to content

Instantly share code, notes, and snippets.

@balaam
Created July 30, 2014 07:12
Show Gist options
  • Select an option

  • Save balaam/ea47ba520cbf40bcfc2e to your computer and use it in GitHub Desktop.

Select an option

Save balaam/ea47ba520cbf40bcfc2e to your computer and use it in GitHub Desktop.
Combinitoric mess
function clone(s)
local t = {}
for k, v in pairs(s) do
t[k] = v
end
return t
end
-- Generate all possible combinations, then filter
-- Reasonably horrible runtime but I want it offline and for a set of 4 so it's not a problem
local i = 0
function Combo(t, filter, output, count)
output = output or {}
count = count or #t
if count == 0 then
table.sort(output)
filter[table.concat(output)] = true
return
end
for k, v in ipairs(t) do
local myoutput = clone(output)
table.insert(myoutput, v)
Combo(t, filter, myoutput, count - 1)
end
end
f = {}
Combo({'a', 'b', 'c', 'd'}, f)
-- Fancy formatter
local final = {}
for k, v in pairs(f) do
table.insert(final, k)
end
table.sort(final)
for k, v in ipairs(final) do
print(k, v)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment