Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split a list into two sublists in all possible ways

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.

like image 732
LeoTietz Avatar asked Apr 15 '15 17:04

LeoTietz


People also ask

How do you split a list into two sublists in Python?

Split List Into Sublists Using the array_split() Function in NumPy. The array_split() method in the NumPy library can also split a large array into multiple small arrays. This function takes the original array and the number of chunks we need to split the array into and returns the split chunks.

Can you split a list in Java?

In the Guava library, we can use Lists. partition() method that splits the list into consecutive sublists, every list specified size. In order to split the list into two sublists, In our case, we can pass the size that is equal to half the size of our list.

How can I split an ArrayList in multiple small Arraylists?

import org. List<Integer> inputList = new ArrayList(Arrays. asList(100, 101, 102, 103, 104, 105)); /* Change the value of chunkSize to get desired number of elements in each of the partitionedList */ int chunkSize = 5; List<List<Integer>> partitionedList = ListUtils. partition(inputList, chunkSize);


1 Answers

l = [1, 2, 3, 4]
flags = [False] * len(l)
while True:
    a = [l[i] for i, flag in enumerate(flags) if flag]
    b = [l[i] for i, flag in enumerate(flags) if not flag]
    print a, b
    for i in xrange(len(l)):
        flags[i] = not flags[i]
        if flags[i]:
            break
    else:
        break

Result:

[] [1, 2, 3, 4]
[1] [2, 3, 4]
[2] [1, 3, 4]
[1, 2] [3, 4]
[3] [1, 2, 4]
[1, 3] [2, 4]
[2, 3] [1, 4]
[1, 2, 3] [4]
[4] [1, 2, 3]
[1, 4] [2, 3]
[2, 4] [1, 3]
[1, 2, 4] [3]
[3, 4] [1, 2]
[1, 3, 4] [2]
[2, 3, 4] [1]
[1, 2, 3, 4] []

It can easily be adapted to java:

public static void main(String[] args) {
    int[] l = new int[] { 1, 2, 3, 4 };
    boolean[] flags = new boolean[l.length];
    for (int i = 0; i != l.length;) {
        ArrayList<Integer> a = new ArrayList<>(), b = new ArrayList<>();
        for (int j = 0; j < l.length; j++)
            if (flags[j]) a.add(l[j]); else b.add(l[j]);
        System.out.println("" + a + ", " + b);
        for (i = 0; i < l.length && !(flags[i] = !flags[i]); i++);
    }
}
like image 163
JuniorCompressor Avatar answered Oct 05 '22 12:10

JuniorCompressor