All subsets of an array

Reading time ~1 minute

Finding all possible, non-empty subsets of an array is actually a really simple task. Everything you need is a counter between 1 and 2^n-1, where n is the number of entries. For each possible value of the counter check it’s bits - 1 at position i means that i’th value of the array is present in the particular subset. No recursion, only shifts and additions.

int[] numbers = { 1, 2, 3, 4 };
int n = numbers.length;
int np = 1 << n;
for (int i = 1; i < np; i++) {
	int bitv = i;
	int pos = 0;
	while (bitv > 0) {
		if ((bitv & 1) == 1) {
			System.out.print(numbers[pos] + " ");
		}
		bitv >>= 1;
		pos++;
	}
	System.out.println();
}

On designing and running online experiments

Have you wondered how to design and run online experiments? In particular, how to implement an experiment dashboard such as the one enpic...… Continue reading

Software Engineering at Cxense

Published on April 17, 2017

Having fun with Scikit-Learn

Published on December 01, 2016