每个可变数量的列表使用一个条目连接所有可能的字符串

时间:2015-10-03 10:33:43

标签: list lua

令我困惑的是可变数量的列表。 (伪代码)很容易......

 for i=1 to list1_size
   for ii=1 to list2_size
     for iii=1 to list3_size
        results_list.add(list1[i]+list2[ii]+list3[iii])

...但我可以在正确的方向上轻推一下如何使用不同数量的列表进行此操作。

我已经开始获取每个列表中的条目数量表,并且看起来确实需要一些递归,但是从那里我画了一个空白。

编辑:澄清一下,我在寻找的是......

输入:具有不同数量条目的不同数量的列表/表格。

{"A","B","C"}  {"1","2","3","4"}  {"*","%"}

输出:

{"A1*", "A1%", "A2*", "A2%", "A3*", "A3%", "A4*", "A4%",
 "B1*", "B1%", "B2*", "B2%", "B3*", "B3%", "B4*", "B4%",
 "C1*", "C1%", "C2*", "C2%", "C3*", "C3%", "C4*", "C4%"}

1 个答案:

答案 0 :(得分:3)

local list_of_lists = {{"A","B","C"},  {"1","2","3","4"},  {"*","%"}}

local positions = {}
for i = 1, #list_of_lists do
   positions[i] = 1
end

local function increase_positions()
   for i = #list_of_lists, 1, -1 do
      positions[i] = positions[i] % #list_of_lists[i] + 1
      if positions[i] > 1 then return true end
   end
end

local function get_concatenated_selection()
   local selection = {}
   for i = 1, #list_of_lists do
      selection[i] = list_of_lists[i][positions[i]]
   end
   return table.concat(selection)
end

local result = {}
repeat
   table.insert(result, get_concatenated_selection())
until not increase_positions()