Last active
November 9, 2017 12:46
-
-
Save PRotondo/e36af6724e2ab6c4f1aea0234649c8dd to your computer and use it in GitHub Desktop.
Iterate over each element of a set in order. It is well-known that one can easily do it in reverse order by doing i = (i-1)&superset (see https://www.topcoder.com/community/data-science/data-science-tutorials/a-bit-of-fun-fun-with-bits/)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def binary(x) : | |
return format(x,'b') | |
def print_binary(x) : | |
print binary(x) | |
def iterate(superset,f) : | |
i = 0 | |
while True : | |
f(i) | |
if i == superset : | |
break | |
i = ((i|(~superset))+1)&superset | |
iterate(53,print_binary) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment