There is a lot of quick methods on stack overflow that generate all the combination of bitsets of size k.
Ex:
combos(4,3) = {1110,1101,1011,0111}
However, is there a quick way to calculate all combos for sizes 1 to n?
Ex:
allCombos(4) =
{
1: {0001, 0010, 0100, 1000}
2: {0011, 0101, 1001, 0110, 1010, 1100}
3: {0111, 1011, 1101, 1110}
4: {1111}
}
Of course, I can just call combos(4, i)
for i from 1 to 4. However, I want to take advantage of the following fact:
For each bitset X
in combos(4, i)
, we can set exactly one bit from bitset X
from 0 to 1.
For example, 0011
is in combos(4,2)
which generates {1011, 0111}
in combos(4,3)
since the 1st and 2nd most significant bits are 0's.
EDIT: here is my current way of accomplishing this:
def combos(set_, k):
return map(sum, itertools.combinations(set_, k)) # take combos of 1, 2, 4, 8 ... and the sums to get bitset back
def allCombos(n):
ret = {}
bitset = set([2**i for i in range(n)]) # generates 1, 2, 4, 8 ...
for k in range(1,n+1):
ret[k] = combos(bitset, k)
return k