0

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?

  • Those are bitwise operators; [this question](https://stackoverflow.com/questions/1746613/bitwise-operation-and-usage) might clear up some doubts – Ralf Aug 30 '19 at 14:27
  • 1
    Or this link https://wiki.python.org/moin/BitwiseOperators – Ralf Aug 30 '19 at 14:29

0 Answers0