6
1
Write a program or function that takes a base n
and a number x
, and returns or outputs the n
-ary Gray code of x
.
Rules
n
is an integer in the range2 <= n <= 16
.x
is an integer in the range0 <= n < 2**32
.- The inputted numbers can either be two integer arguments if you write a function, or two arguments to
ARGV
or a space separated pair toSTDIN
if you write a program. - The output should be a string, either returned or printed to
STDOUT
. - If
n
is 10 or more, use hex digits to represent digits with a value greater than 9. The case of the digits does not matter. - If the
n
-ary Gray code ofx
is zero,'0'
must be printed or returned. - Code length is in bytes, and standard loopholes are not permitted.
- You do not need to use the same algorithm as below, but if you use a different one, make sure it produces valid Gray codes.
Example
Example code adapted from Wikipedia:
>>> import string
>>>
>>> def nary_gray_code(base, num):
... base_num = []
... # Convert to num to base base
... while num:
... base_num.append(num % base)
... num = num // base
... # Convert base base number to base-ary Gray code
... shift = 0
... gray = []
... for b in reversed(base_num):
... gray.insert(0, (b + shift) % base)
... shift += base - gray[0]
... return "".join(string.hexdigits[i] for i in reversed(gray)) or "0"
...
>>> nary_gray_code(2, 100)
'1010110'
>>> nary_gray_code(2, 1)
'1'
>>> nary_gray_code(2, 0)
'0'
>>> nary_gray_code(10, 23)
'21'
>>> nary_gray_code(10, 100)
'190'
>>> nary_gray_code(3, 26)
'200'
>>> nary_gray_code(3, 27)
'1200'
>>> for i in range(2, 17):
... print(i, nary_gray_code(i, 2**32 - 1))
...
2 10000000000000000000000000000000
3 122102120011102000122
4 3000000000000000
5 34020102231331
6 1401154214013
7 260241350125
8 34000000000
9 11762782617
10 4875571576
11 1824007509
12 92b627446
13 5b25a2c00
14 2ac96ab2b
15 197dde1a7
16 f0000000
>>>
Keep in mind that there are many different gray codes, so you can't test through equality with some test set. – orlp – 2015-04-15T21:15:42.343