Skip to content

Instantly share code, notes, and snippets.

@joshgrib
Created April 7, 2016 01:51
Show Gist options
  • Save joshgrib/8bf7b35d21ac3b879bb1799665109e89 to your computer and use it in GitHub Desktop.
Save joshgrib/8bf7b35d21ac3b879bb1799665109e89 to your computer and use it in GitHub Desktop.
#generated binary reflected gray codes in two ways
def BRGC(n):
"""
Return list of binary reflected gray codes changing one bit at a time
"""
if n is 1:
return ['0', '1']
else:
L1 = BRGC(n-1)
L2 = L1[::-1]
L1 = ['0' + x for x in L1]
L2 = ['1' + y for y in L2]
L = L1 + L2
return L
def BRGC2(n):
"""
Return list of binary reflected gray codes in binary order
"""
if n is 1:
return ['0', '1']
else:
L1 = BRGC2(n-1)
L2 = L1
L1 = ['0' + x for x in L1]
L2 = ['1' + y for y in L2]
print n
print L1
print L2
print ''
L = L1 + L2
return L
print BRGC(3)
print BRGC2(3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment