36
4
Introduction
A Gray Code is an alternative to binary representation in which a number is incremented by toggling only one bit, rather than a variable amount of bits. Here are some gray codes along with their decimal and binary equivalents:
decimal | binary | gray
-------------------------
0 | 0 | 0
-------------------------
1 | 1 | 1
-------------------------
2 | 10 | 11
-------------------------
3 | 11 | 10
-------------------------
4 | 100 | 110
-------------------------
5 | 101 | 111
-------------------------
6 | 110 | 101
-------------------------
7 | 111 | 100
-------------------------
8 | 1000 | 1100
-------------------------
9 | 1001 | 1101
-------------------------
10 | 1010 | 1111
-------------------------
11 | 1011 | 1110
-------------------------
12 | 1100 | 1010
-------------------------
13 | 1101 | 1011
-------------------------
14 | 1110 | 1001
-------------------------
15 | 1111 | 1000
Cyclic Bit Pattern of a Gray Code
Sometimes called "reflected binary", the property of changing only one bit at a time is easily achieved with cyclic bit patterns for each column starting from the least significant bit:
bit 0: 0110011001100110011001100110011001100110011001100110011001100110
bit 1: 0011110000111100001111000011110000111100001111000011110000111100
bit 2: 0000111111110000000011111111000000001111111100000000111111110000
bit 3: 0000000011111111111111110000000000000000111111111111111100000000
bit 4: 0000000000000000111111111111111111111111111111110000000000000000
bit 5: 0000000000000000000000000000000011111111111111111111111111111111
...and so on.
Objective
Given a non-padded input string of a gray code, increment the gray code by alternating a single character in the sequence or prepending a 1
(when incrementing to the next power of 2), then output the result as a non-padded gray code.
Caveats
- Do not worry about taking
0
or an empty string as input. - The lowest input will be
1
, and there is no upper-bound to the string length other than memory limitations imposed by the environment. - By non-padded string, I mean there will be no leading or trailing whitespace (other than an optional trailing newline), and no leading
0
s in the input or output.
I/O formats
The following formats are accepted form for input and output, but strings are encouraged over other formats:
- most significant "bit" first
- non-padded character array or string of ASCII
'1'
s and'0'
s - non-padded integer array of
1
s and0
s - non-padded boolean array
What's not allowed:
- least significant "bit" first
- decimal, binary or unary integer
- fixed-length data-structure
- character array or string of non-printable ASCII indices
1
and0
Tests
input -> output
1 -> 11
11 -> 10
111 -> 101
1011 -> 1001
1111 -> 1110
10111 -> 10110
101100 -> 100100
100000 -> 1100000
More tests can be added by request.
Criteria
This is code-golf, so shortest program in bytes wins! All ties will be broken by favoring earlier submissions; standard loopholes apply. The best submitted answer will be accepted October 9th, 2016, and updated whenever better answers are given.
1Related. Related. – Martin Ender – 2016-10-02T21:08:18.647
Can we take input as a number? – xnor – 2016-10-02T21:12:01.200
1
Less obviously, also related.
– Martin Ender – 2016-10-02T21:12:50.607@xnor No. That sort of defeats the purpose of the string manipulation challenge. – Patrick Roberts – 2016-10-02T21:12:59.233
According to your table (and wikipedia)
1011
is incremented to1001
and not1010
as in the testcases. – Laikoni – 2016-10-02T22:36:38.013@Laikoni good catch, thank you. Corrected. – Patrick Roberts – 2016-10-02T22:42:29.347
Can we take an array of zeros and ones instead of a string? – Luis Mendo – 2016-10-02T22:52:19.090
@LuisMendo, do you mean a boolean array or an integer array of 1s and 0s? Both are acceptable, I'm just curious which one you meant. But please make your program or function have a reasonable output: either equivalent as input, or as specified in the question. – Patrick Roberts – 2016-10-02T22:53:47.380
@PatrickRoberts I meant integer array. Thanks for the reply! – Luis Mendo – 2016-10-02T22:54:17.587
1Can I take both input and output reversed, e.g.
0011
for 8 – Ton Hospel – 2016-10-03T06:32:45.787Can I take input and output as a string of unprintable ASCII codes 0 and 1 – Ton Hospel – 2016-10-03T06:48:53.640
@TonHospel I'm gonna say no, just because that makes it harder to verify code. You might as well use an integer array if you're going to do that. – Patrick Roberts – 2016-10-03T12:17:18.663
Is there a maximum length of input that our program will have to process? I too am curious about whether we can take input reversed. – 0 ' – 2016-10-03T18:29:37.680
@1000000000 To demonstrate that the program can process an arbitrarily long string, make sure it works for a string input at least 65 bytes long. Also, no. It must be most significant bit first. – Patrick Roberts – 2016-10-03T18:37:34.150
@PatrickRoberts and it's okay if it behaves unexpectedly for a string longer than 65 bytes? – 0 ' – 2016-10-03T18:39:34.590
@1000000000 No. As I stated in the challenge, the only limitation should be the amount of memory your program is able to allocate to process the string. If your run exits with an out of memory error in the case the string is too long, that is the only acceptable "unexpected behavior." In short, don't hardcode an upper-bound. – Patrick Roberts – 2016-10-03T18:41:54.870
1@TonHospel sorry I didn't see your question about reversed I/O. As I told 1000000000 my answer is no. – Patrick Roberts – 2016-10-03T18:45:41.167
@TonHospel I just thought of something though. If your input is stack-based and each bit (or character) is pushed separately, you'd have access to the least significant bit first, and it would satisfy my requirement of most significant bit first. Assuming FILO stack. – Patrick Roberts – 2016-10-04T16:41:57.347