[Snippet] Permutations

stable
By Strooth in Miscellaneous Published September 2021 👁 1,243 views 💬 2 comments

Description

Example Snippet to generate all permuations/combinations from m, n where m is the length of the permutation and n the available numbers.
HaasScript
function unpack (t, i)
  i = i or 1
  if t[i] ~= nil then
    return t[i], unpack(t, i + 1)
  end
end
function map(f, a, ...) if a then return f(a), map(f, ...) end end
function incr(k) return function(a) return k > a and a or a+1 end end
function combs(m, n)
  if m * n == 0 then return {{}} end
  local ret, old = {}, combs(m-1, n-1)
  for i = 1, n do
    for k, v in pairs(old) do ret[#ret+1] = {i, map(incr(i), unpack(v))} end
  end
  return ret
end

list = {}
for k, v in pairs(combs(4, 10)) do list = ArrayAdd(list, {unpack(v)}) end

list = ArrayDistinct(list)
for k,v in pairs(list) do 
    Log(v)
end

2 Comments

Sign in to leave a comment.

H
Hedgehog1729 over 4 years ago

This is really cool; How can this be used to launch backtest or simulations?

S
Strooth over 4 years ago

I'm not sure it can, at least not from the script editor that I am aware of. Via the trade server api you can call backtests however, there are already some tools for that by other people if you search in the discord.