Skip to content

Instantly share code, notes, and snippets.

@rajadain
Created December 7, 2012 05:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rajadain/4230991 to your computer and use it in GitHub Desktop.
Save rajadain/4230991 to your computer and use it in GitHub Desktop.
Graycode generator
def graycode(n):
a = [0]*n
for i in range(0,pow(2,n)):
b = 'X'
for j in range(0,n):
if ((i % pow(2,j+1)) - pow(2,j) == 0):
a[j] = pow(1 - a[j], 2)
b = j
break
print i, "".join((map(str,a)))[::-1], b
graycode(10)
@rajadain
Copy link
Author

rajadain commented Dec 7, 2012

This gist goes with my blog post Generate Gray Codes in Python.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment