Write a generator that returns every arrangement of items such that each is in one or none of two different bags.
Each combination should be given as a tuple of two lists, the first being the items in bag1, and the second being the items in bag2.
def yieldAllCombos(items):
N = len(items)
for i in xrange(3**N):
bag1 = []
bag2 = []
for j in xrange(N):
# test bit jth of integer i
if (i/3**j) % 3 == 1:
bag1.append(items[j])
if (i/3**j) % 3 == 2:
bag2.append(items[j])
yield (bag1,bag2)
items = [1,7,3]
k=0
for i in yieldAllCombos(items):
print i
k+=1
print k
result was printed as following:
([], [])
([1], [])
([], [1])
([7], [])
([1, 7], [])
([7], [1])
([], [7])
([1], [7])
([], [1, 7])
([3], [])
([1, 3], [])
([3], [1])
([7, 3], [])
([1, 7, 3], [])
([7, 3], [1])
([3], [7])
([1, 3], [7])
([3], [1, 7])
([], [3])
([1], [3])
([], [1, 3])
([7], [3])
([1, 7], [3])
([7], [1, 3])
([], [7, 3])
([1], [7, 3])
([], [1, 7, 3])
27