-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path06-05_array_elem_combination.lua
More file actions
46 lines (42 loc) · 1.2 KB
/
06-05_array_elem_combination.lua
File metadata and controls
46 lines (42 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
function getCombinations(array, prevCombinations, firstIteration)
--debug.debug()
printCombinations(prevCombinations)
for i = 1, #array do
currCombinations = {}
if firstIteration then
table.insert(currCombinations, {array[i]})
else
for j = 1, #prevCombinations do
local newComb = table.move(prevCombinations[j], 1, #prevCombinations[j], 1, {})
table.insert(newComb, array[i])
table.insert(currCombinations, newComb)
end
end
newArray = table.move(array, 1, #array, 1, {})
table.remove(newArray, i)
getCombinations(newArray, currCombinations, false)
end
end
function printCombinations(combinations)
for i = 1, #combinations do
printArray(combinations[i])
if(i ~= #combinations) then
io.write(", ")
end
end
end
function printArray(array)
io.write("{")
for i = 1, #array do
io.write(array[i])
if(i ~= #array) then
io.write(", ")
end
end
io.write("}\n")
end
array={}
for elem in string.gmatch(arg[1], "[^,]+") do
table.insert(array,elem)
end
getCombinations(array, {}, true)