I need to create all combinations of a list but each element has a lower and upper boundary (increasing with each digit by +2).
For example with n=4: All combinations from [0, 1, 2, 3] (lower boundaries) to [0, 2, 4, 6] (upper boundaries) should result in:
[[0, 1, 2, 3],
[0, 1, 2, 4],
[0, 1, 2, 5],
[0, 1, 2, 6],
[0, 1, 3, 4],
[0, 1, 3, 5],
[0, 1, 3, 6],
[0, 1, 4, 5],
[0, 1, 4, 6],
[0, 2, 3, 4],
[0, 2, 3, 5],
[0, 2, 3, 6],
[0, 2, 4, 5],
[0, 2, 4, 6]]
The straight forward solution is to use itertools.combinations(range(2*n-1),n) and then filter out all non-valid. But that creates lots of invalid combinations first and then is further slowed down by a filter run through all. That is too inefficient for large n in my case.
I need a solution that does not even attempt to loop higher for each digit than possible and only create combinations within the boundaries for each digit.
I'm sure there is a simple solution with nested loops or backtracking but I can't find it.