Lua 找到这两组项目的所有可能组合?卢阿

Lua 找到这两组项目的所有可能组合?卢阿,lua,lua-table,Lua,Lua Table,我在Haskell和Python等不同编程语言中看到了类似的问题答案,但它们都使用Lua没有的内置功能,所以请不要将此问题标记为重复 假设我有两张像bellow这样的桌子: table1 = {A,B,C} table2 = {D,E,F} 我想找到匹配两个表中项目的所有独特方法,答案应该是(用非正式符号): 因此答案将存储在一个表中,表[1]将是{a,D},{B,E},{C,F}等等 表的长度可以是任意长度,但两个表的大小相同。我们可以通过归纳(不是最快的方法,但很容易编写/理解)得到所

我在Haskell和Python等不同编程语言中看到了类似的问题答案,但它们都使用Lua没有的内置功能,所以请不要将此问题标记为重复

假设我有两张像bellow这样的桌子:

table1 = {A,B,C}
table2 = {D,E,F}  
我想找到匹配两个表中项目的所有独特方法,答案应该是(用非正式符号):

因此答案将存储在一个表中,表[1]将是
{a,D},{B,E},{C,F}
等等
表的长度可以是任意长度,但两个表的大小相同。

我们可以通过归纳(不是最快的方法,但很容易编写/理解)得到所有的洗牌


另一种方法是使用以下代码。写这篇文章是为了帮助你玩一个游戏(排版转换),找出所有可能的字母组合。不过,我已经对它进行了修改,以适合您的示例

-- table array: { {1, 2}, {3, 4}, {5, 6} }
-- Should return { 135, 136, 145, 146, 235, 236, 245, 246 }
--
-- This uses tail recursion so hopefully lua is smart enough not to blow the stack
function arrayCombine(tableArray)
  -- Define the base cases
  if (tableArray == nil) then
      return nil
  elseif (#tableArray == 0) then
      return {}
  elseif (#tableArray == 1) then
      return tableArray[1]
  elseif (#tableArray == 2) then
      return arrayCombine2(tableArray[1], tableArray[2])
    end -- if

  -- We have more than 2 tables in the input parameter.  We want to pick off the *last*
  -- two arrays, merge them, and then recursively call this function again so that we 
  -- can work our way up to the front.
  local lastArray = table.remove(tableArray, #tableArray)
  local nextToLastArray = table.remove(tableArray, #tableArray)
  local mergedArray = arrayCombine2(nextToLastArray, lastArray)

  table.insert(tableArray, mergedArray)

  return arrayCombine(tableArray)
end -- arrayCombine


function arrayCombine2(array1, array2)
  local mergedArray = {}

  for _, elementA in ipairs(array1) do
    for _, elementB in ipairs(array2) do
      table.insert(mergedArray, elementA .. elementB) 
    end -- for
  end -- for

  return mergedArray
end -- arrayCombine2

-- You can set it up this way:
combinedArray = {}
table.insert(combinedArray, {"A", "B", "C"})
table.insert(combinedArray, {"D", "E", "F"})

for i,v in ipairs(arrayCombine(combinedArray)) do
  print(i,v)
end

-- Or go this way, which may be somewhat cleaner:

for i,v in ipairs(arrayCombine({{"A", "B", "C"}, {"D", "E", "F"}})) do
  print(i,v)
end
无论哪种方式,它都会产生您想要的结果。

函数获取所有组合(arr1、arr2)
function get_all_combinations(arr1, arr2)
   local n, e, all_comb  = #arr1, {}, {}
   for j = 1, n do
      e[j] = arr2[j]
   end
   local function generate(m)
      if m <= 1 then
         local comb = {}
         all_comb[#all_comb + 1] = comb
         for j = 1, n do
            comb[j] = arr1[j]..e[j]  -- it should be {arr1[j], e[j]} to fulfill your requirements
         end
      else
         for j = 1, m do
            generate(m - 1)
            local k = j < m and m % 2 == 1 and 1 or j
            e[k], e[m] = e[m], e[k]
         end
      end
   end
   generate(n)
   return all_comb
end

for i, v in ipairs(get_all_combinations({"A", "B", "C"}, {"D", "E", "F"})) do
  print(i, table.concat(v, ";"))
end
局部n,e,all#comb=#arr1,{},{} 对于j=1,n do e[j]=arr2[j] 结束 局部函数生成(m)
如果我感谢你的解决方案,但是这种方法非常耗时,特别是对于较大的桌子,我想在游戏中使用它,所以,尽管你的解决方案有效,但我不能使用它。对于大小
N
的桌子,有
N的组合,虽然洗牌可以计算得更快,但我怀疑它可以计算得更快。因此,更好的方法是设置n个上界和预混混洗,(a)在文件中存储混洗并读取它(b),通过编译的C++模板实现Stuffle计算,不认为我产生了我所寻找的结果,请检查Andrew Kashpur的代码,看看结果是什么,我好像误读了这个意图。您正在寻找可以创建的不同集合,而不是单个元素的不同组合。这就是我一大早读东西的原因。让我看看我能不能想出一些和你现在要求的完全相符的东西。。。
-- table array: { {1, 2}, {3, 4}, {5, 6} }
-- Should return { 135, 136, 145, 146, 235, 236, 245, 246 }
--
-- This uses tail recursion so hopefully lua is smart enough not to blow the stack
function arrayCombine(tableArray)
  -- Define the base cases
  if (tableArray == nil) then
      return nil
  elseif (#tableArray == 0) then
      return {}
  elseif (#tableArray == 1) then
      return tableArray[1]
  elseif (#tableArray == 2) then
      return arrayCombine2(tableArray[1], tableArray[2])
    end -- if

  -- We have more than 2 tables in the input parameter.  We want to pick off the *last*
  -- two arrays, merge them, and then recursively call this function again so that we 
  -- can work our way up to the front.
  local lastArray = table.remove(tableArray, #tableArray)
  local nextToLastArray = table.remove(tableArray, #tableArray)
  local mergedArray = arrayCombine2(nextToLastArray, lastArray)

  table.insert(tableArray, mergedArray)

  return arrayCombine(tableArray)
end -- arrayCombine


function arrayCombine2(array1, array2)
  local mergedArray = {}

  for _, elementA in ipairs(array1) do
    for _, elementB in ipairs(array2) do
      table.insert(mergedArray, elementA .. elementB) 
    end -- for
  end -- for

  return mergedArray
end -- arrayCombine2

-- You can set it up this way:
combinedArray = {}
table.insert(combinedArray, {"A", "B", "C"})
table.insert(combinedArray, {"D", "E", "F"})

for i,v in ipairs(arrayCombine(combinedArray)) do
  print(i,v)
end

-- Or go this way, which may be somewhat cleaner:

for i,v in ipairs(arrayCombine({{"A", "B", "C"}, {"D", "E", "F"}})) do
  print(i,v)
end
function get_all_combinations(arr1, arr2)
   local n, e, all_comb  = #arr1, {}, {}
   for j = 1, n do
      e[j] = arr2[j]
   end
   local function generate(m)
      if m <= 1 then
         local comb = {}
         all_comb[#all_comb + 1] = comb
         for j = 1, n do
            comb[j] = arr1[j]..e[j]  -- it should be {arr1[j], e[j]} to fulfill your requirements
         end
      else
         for j = 1, m do
            generate(m - 1)
            local k = j < m and m % 2 == 1 and 1 or j
            e[k], e[m] = e[m], e[k]
         end
      end
   end
   generate(n)
   return all_comb
end

for i, v in ipairs(get_all_combinations({"A", "B", "C"}, {"D", "E", "F"})) do
  print(i, table.concat(v, ";"))
end