I have a list of variable size, for example
[1, 2, 3, 4]
and I want to get every possible way to split this list into two:
([], [1, 2, 3, 4])
([1], [2, 3, 4])
([2], [1, 3, 4])
([3], [1, 2, 4])
([4], [1, 2, 3])
([1, 2], [3, 4])
([1, 3], [2, 4])
([1, 4], [2, 3])
([2, 3], [1, 4])
([2, 4], [1, 3])
([3, 4], [1, 2])
([1, 2, 3], [4])
([1, 2, 4], [3])
([1, 3, 4], [2])
([2, 3, 4], [1])
([1, 2, 3, 4], [])
I'm pretty sure this is not an unknown problem and there is probably an algorithm for this, however I could not find one. Also, this should not use any external libraries but work with simple language features (loops, conditions, methods/functions, variables, ...) found in most languages.
I've written a hackish solution in Python:
def get_all(objects):
for i in range(1, len(objects)):
for a in combinations(objects, i):
for b in combinations([obj for obj in objects if obj not in up], len(objects) - i):
yield State(up, down)
if objects:
yield State([], objects)
yield State(objects, [])
However, it uses library features and is not very nice looking in general.