Last active
December 18, 2015 08:59
-
-
Save st0le/5758462 to your computer and use it in GitHub Desktop.
Gray Codes
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
""" | |
For a +ve integer n, print numbers from 0 to 2^n-1 such that numbers next to each other differ in exactly 1 bit. | |
""" | |
n = 3 | |
gray = lambda x : (x >> 1) ^ x | |
pad_left = lambda s,l: ''.join(['0']*(l-len(s))) + s | |
for i in xrange(1 << n): | |
print i,pad_left(bin(gray(i))[2:],n) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment