Skip to content

Instantly share code, notes, and snippets.

@JeffreyMFarley
Created December 16, 2015 20:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JeffreyMFarley/8c044a2040f52af23c43 to your computer and use it in GitHub Desktop.
Save JeffreyMFarley/8c044a2040f52af23c43 to your computer and use it in GitHub Desktop.
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