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

时间:2019-11-02 07:10:36

标签: lua lua-table

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

假设我有两个像波纹管这样的桌子:

table1 = {A,B,C}
table2 = {D,E,F}  

我想找到匹配两个表中项目的所有唯一方式,答案应该是(用非正式表示法):

AD,BE,CF
AD,BF,CE
AE,BD,CF
AE,BF,CD
AF,BD,CE
AF,BE,CD  

因此答案将存储在表中,其中table [1]将为{{A, D}, {B, E}, {C, F}},依此类推。
表格长度可以是任何长度,但两者的大小都相同。

3 个答案:

答案 0 :(得分:1)

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

 const [reopenInvoice, { loading: reopenLoading, data: reopenData, error: reopenError }] = useMutation<IReopenData, IReopenVariables>(
        INVOICE_CLONE,
        {
            onCompleted: ({ invoiceClone: { errors, status } }) => {
                if (!errors || !errors.length) {
                    message.success("Invoice Reopened");
                } else {
                    message.error(errors.join(" "));
                }
            },
            refetchQueries: [
                {
                    query: TO_REFETCH_QUERY,
                    variables: {
                        id: objectID,
                    },
                },
            ],
        }
    );

答案 1 :(得分:1)

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

答案 2 :(得分:0)

执行此操作的另一种方法是使用以下代码。编写该文档是为了帮助游戏(类型转换)来发现字母的可变组的所有可能组合。不过,我已经对其进行了修改以适合您的示例。

-- 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

无论哪种方式,它都能产生您想要的结果。