Created
September 8, 2013 08:04
-
-
Save jimexist/6482798 to your computer and use it in GitHub Desktop.
Generate grey code
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
#!/usr/bin/env python | |
import sys | |
def toGrey(num): | |
return (num >> 1) ^ num | |
def generate(n): | |
for i in xrange(2**n): | |
yield toGrey(i) | |
if __name__ == '__main__': | |
n = int(sys.argv[1]) | |
for i in generate(n): | |
print bin(i)[2:].zfill(n) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment