Created
December 16, 2015 20:13
-
-
Save JeffreyMFarley/8c044a2040f52af23c43 to your computer and use it in GitHub Desktop.
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 genGrayCode(n): | |
if n == 1: | |
return ['0', '1'] | |
else: | |
lastCode = genGrayCode(n - 1) | |
rLastCode = lastCode[:] | |
rLastCode.reverse() | |
lastCode = ['0' + x for x in lastCode] | |
rLastCode = ['1' + x for x in rLastCode] | |
return lastCode + rLastCode | |
for i, code in enumerate(genGrayCode(8)): | |
print(i, int(code, 2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment