I have a 2d list where the number of rows is between 1 to 5, and a number of elements in each row are dynamic. Let's suppose I have a list like
values = [[1,2,3], [4,5],[6,7,8,9]];
result = [[1,4,6],[1,4,7],[1,4,8],[1,4,9],[1,5,6],[1,5,7],[1,5,8],[1,5,9],[2,4,6],[2,4,7],[2,4,8],[2,4,9],[2,5,6],[2,5,7],[2,5,8],[2,5,9],[3,4,6],[3,4,7],[3,4,8],[3,4,9],[3,5,6],[3,5,7],[3,5,8],[3,5,9]]
values are my input and I need the output in the format of the result.
I have tried implementing a function in a recursive fashion but failed. My function is
import groovy.transform.Field
List lines = [];
def function(row, col, lines)
{
if(row >= values.size())
{
log.info("This should go inside result: " + lines);
return;
}
if(col >= values[row].size())
{
return;
}
lines << values[row][col];
function(row+1,col,lines);
lines.pop();
function(row,col+1,lines);
return;
}
@Field
List values = [[1,2,3],[4,5],[6,7,8,9]];
@Field
List lst = [];
for(int i=0; i<values[0].size(); i++)
function(0, i, lines)
Groovy has a built-in collection function combinations()
that generates all possible combinations from a list of lists of elements.
Example usage:
assert [['a', 'b'],[1, 2, 3]].combinations() == [['a', 1], ['b', 1], ['a', 2], ['b', 2], ['a', 3], ['b', 3]]
You can simply invoke this method on values
from your initial script:
def values = [[1,2,3], [4,5],[6,7,8,9]]
def expected = [[1,4,6],[1,4,7],[1,4,8],[1,4,9],[1,5,6],[1,5,7],[1,5,8],[1,5,9],[2,4,6],[2,4,7],[2,4,8],[2,4,9],[2,5,6],[2,5,7],[2,5,8],[2,5,9],[3,4,6],[3,4,7],[3,4,8],[3,4,9],[3,5,6],[3,5,7],[3,5,8],[3,5,9]]
def result = values.combinations()
assert ((expected as Set) == (result as Set))
import groovy.transform.CompileStatic
import groovy.transform.TailRecursive
@TailRecursive
@CompileStatic
<T> List<List<T>> combinations(final List<List<T>> xss, final List<List<T>> result = [[]]) {
return !xss ? result : combinations(xss.tail(), process(xss.head(), result))
}
@CompileStatic
<T> List<List<T>> process(final List<T> xs, final List<List<T>> result) {
return result.inject([]) { yss, ys -> yss + xs.collect { x -> ys + x } }
}
def values = [[1,2,3], [4,5],[6,7,8,9]]
def expected = [[1,4,6],[1,4,7],[1,4,8],[1,4,9],[1,5,6],[1,5,7],[1,5,8],[1,5,9],[2,4,6],[2,4,7],[2,4,8],[2,4,9],[2,5,6],[2,5,7],[2,5,8],[2,5,9],[3,4,6],[3,4,7],[3,4,8],[3,4,9],[3,5,6],[3,5,7],[3,5,8],[3,5,9]]
assert combinations(values) == expected
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With