Created
August 14, 2012 21:01
-
-
Save balaam/3353022 to your computer and use it in GitHub Desktop.
Get all combinations and all sub combinations in a table (I'm sure there's a term for this!)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function AllCombinations(list, stop) | |
| stop = stop or #list | |
| if stop == 0 then | |
| return list | |
| end | |
| local collect = {} | |
| for k, v in pairs(list) do | |
| if type(v) ~= "table" then | |
| table.insert(collect, {v}) | |
| else | |
| table.insert(collect, v) | |
| end | |
| local results = AllCombinations(list, stop - 1) | |
| for i, j in pairs(results) do | |
| if type(j) ~= "table" then | |
| j = {j} | |
| end | |
| table.insert(j, v) | |
| table.insert(collect, j) | |
| end | |
| end | |
| return collect | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment