Simple Recursive Generator Question

Skip Montanaro skip at pobox.com
Fri Dec 19 22:08:12 CET 2003


    jcb> This is what I have, but it does not work.

    jcb> def bitIndexGenerator(mask, index=0):
    jcb>     if mask == 0: return
    jcb>     elif mask & 0x1: yield index
    jcb>     bitIndexGenerator(mask >> 1, index+1)

Try:

    def bitIndexGenerator(mask, index=0):
        if mask == 0:
            return
        elif mask & 0x1:
            yield index
        for index in bitIndexGenerator(mask >> 1, index+1):
            yield index

Skip





More information about the Python-list mailing list