I was doing some code from a MIT course and I got this code as a help function, but there are some parts that are a lil obscure from my python understanding.
def partitions(set_):
if not set_:
yield []
return
for i in range(2**len(set_)//2):
parts = [set(), set()]
for item in set_:
parts[i&1].add(item)
i >>= 1
for b in partitions(parts[1]):
yield [parts[0]]+b
I undrstand that it make lots of different combinations of a list, a dict or other iterating obj.
I got my eyes rolling about this parts:
parts[i&1].add(item)
i >>= 1
Some of the fellows here could try to explain to me this part? I look for some in the net, but not sure if I understand completly.
Thanks in advance!
PS- There are any way to make a more readble and using biult in functions like permutations, combinations?