Skip to main content
move [] to save 5 bytes
Source Link
jq170727
  • 531
  • 2
  • 5

Jq 1.5, 105105 100 bytes

def g(n):if n<2then. else map([0]+.)+(reverse|map([1]+.))|g(n-1)end;[[0],[1]]|g(N)|map(map[]|map("\(.)")|add)[]

Assumes N provides input. e.g.

def N: 3 ;

Expanded

def g(n):  # recursively compute gray code
  if n < 2
  then .
  else map([0]+.) + (reverse|map([1]+.)) | g(n-1)
  end;

  [[0],[1]]                 # initial state
| g(N)[]                    # for each #element computein array of gray codes
| map(map("\(.)")|add)[]           # covert to sequence ofa stringsstring

Try it online!Try it online!

Jq 1.5, 105 bytes

def g(n):if n<2then. else map([0]+.)+(reverse|map([1]+.))|g(n-1)end;[[0],[1]]|g(N)|map(map("\(.)")|add)[]

Assumes N provides input. e.g.

def N: 3 ;

Expanded

def g(n):  # recursively compute gray code
  if n < 2
  then .
  else map([0]+.) + (reverse|map([1]+.)) | g(n-1)
  end;

  [[0],[1]]                 # initial state
| g(N)                      # compute array
| map(map("\(.)")|add)[]    # covert to sequence of strings

Try it online!

Jq 1.5, 105 100 bytes

def g(n):if n<2then. else map([0]+.)+(reverse|map([1]+.))|g(n-1)end;[[0],[1]]|g(N)[]|map("\(.)")|add

Assumes N provides input. e.g.

def N: 3 ;

Expanded

def g(n):  # recursively compute gray code
  if n < 2
  then .
  else map([0]+.) + (reverse|map([1]+.)) | g(n-1)
  end;

  [[0],[1]]                 # initial state
| g(N)[]                    # for each element in array of gray codes
| map("\(.)")|add           # covert to a string

Try it online!

Source Link
jq170727
  • 531
  • 2
  • 5

Jq 1.5, 105 bytes

def g(n):if n<2then. else map([0]+.)+(reverse|map([1]+.))|g(n-1)end;[[0],[1]]|g(N)|map(map("\(.)")|add)[]

Assumes N provides input. e.g.

def N: 3 ;

Expanded

def g(n):  # recursively compute gray code
  if n < 2
  then .
  else map([0]+.) + (reverse|map([1]+.)) | g(n-1)
  end;

  [[0],[1]]                 # initial state
| g(N)                      # compute array
| map(map("\(.)")|add)[]    # covert to sequence of strings

Try it online!