Guess data type - format

I currently try to guess what data type the following raw bytes have.
25B3 E548 0200 0000 0003 BF1F D44B 217C 5440 B64E 74B6 E16B 5A40 8B54 F861 F2A9 5440 68C3 95DF C88A 5A40 1021 9EB2 16D6 5440 374D 0D3C FBAB 5A40 01C2 1815 6400 5540 D628 2EA3 4ECF 5A40
It is saved in a variable called outlinePath and has something to do with shapes. The raw bytes were encoded in base64 with the NSKeyedArchiver.
I already tried a float32 array and int16. However, non of both makes sense.
This is the code I came up with. This code works fine in other parts of the dataset:
/**
* Convert the base64 encoded float32 array to actually float32 values
* #param buffer base64 encoded string
*/
export function convertBase64Floats(buffer: string) {
// Decode base64
const decodedBuffer = new Buffer(buffer, 'base64');
// Float32 => 4 bytes
// Get number of float values
const floatValuesCount = decodedBuffer.length / 4;
// New array with correct floats
const arrayOfFloats: number[] = [];
// Get float32s from buffer
for (let i = 0; i < floatValuesCount; i++) {
const offset = i * 4;
const float = decodedBuffer.readFloatLE(offset);
arrayOfFloats.push(float);
}
// Return new array of correct float values
return arrayOfFloats;
}
export function convertInt32Array(buffer: Buffer, useBE: boolean = false) {
const intValuesCount = buffer.length / 4;
const arrayOfInts: number[] = [];
for (let i = 0; i < intValuesCount; i++) {
const offset = i * 4;
const float = useBE ? buffer.readInt32BE(offset) : buffer.readInt32LE(offset);
arrayOfInts.push(float);
}
return arrayOfInts;
}
/**
* Convert the base64 encoded int32 array to actually int32 values
* #param buffer base64 encoded string
* #param useBE use readInt32BE instead of readInt32LE
*/
export function convertBase64Integers(buffer: string, useBE: boolean = false) {
// Decode base64
const decodedBuffer = new Buffer(buffer, 'base64');
// Int32 => 4 bytes
// Get number of integer values
const intValuesCount = decodedBuffer.length / 4;
// New array with correct floats
const arrayOfInts: number[] = [];
// Get float32s from buffer
for (let i = 0; i < intValuesCount; i++) {
const offset = i * 4;
const float = useBE ? decodedBuffer.readInt32BE(offset) : decodedBuffer.readInt32LE(offset);
arrayOfInts.push(float);
}
// Return new array of correct float values
return arrayOfInts;
}
The same dataset also contains the variable strokePath, which looks similar. Also an array with coordinates, which is called extremePoints.
Here is a link to the full dataset, encoded as json: https://pastebin.com/sadZAvyS
The dataset represents the following shape:
Maybe someone with more experience with geometric shapes could explain to me, how this data structure represents the partial shape.
Edit: I just noticed, this could also be the data for just one small element in the partial shape.

Not yet an answer but a start:
Looking at the hexdump -c of outlinePath and strokePath, the data looks structured (not compressed) which leaves a hope to decode it :
0000000 % ³ å H 002 \0 \0 \0 \0 003 ¿ 037 Ô K ! |
0000010 T # ¶ N t ¶ á k Z # 213 T ø a ò ©
0000020 T # h à 225 ß È 212 Z # 020 ! 236 ² 026 Ö
0000030 T # 7 M \r < û « Z # 001 Â 030 025 d \0
0000040 U # Ö ( . £ N Ï Z #
000004a
and
0000000 % ³ å H 005 \0 \0 \0 \0 003 003 003 003 V G j
0000010 Ì ¶ 7 R # Æ p á 200 © l Y # ¹ D z
0000020 a i » R # æ ä r \t ï 206 Y # 032 ¡ 204
0000030 q \ 9 S # Ê 236 204 034 $ ± Y # u e
0000040 214 w ¯ S # n z z U 0 é Y # § v y
0000050 º i « S # - 4 217 Ì Ü ï Y # B ó l
0000060 Ÿ # § S # Ì   í X Ê ö Y # ª E Ñ
0000070 \b š ¢ S # 033 œ : O \v þ Y # 023 , O
0000080 212 . , S # ª s ² \f 020 Æ Y # Î · 1
0000090 G ¹ ­ R # § A 224 } 5 234 Y # L K î
00000a0 Á t ) R # p 8 Ã 0 š 202 Y # l e Ý
00000b0 = s . R # 210 & O Û + { Y # # 237 §
00000c0 º 4 3 R # 035 L Š Ä Ô s Y # V G j
00000d0 Ì ¶ 7 R # Æ p á 200 © l Y #
00000dd
You'll notice a common 4 bytes header followed by a small number 002 or 005, 4 null bytes, either 1 or 4 times 003, and then either 8 or 26 sequences of 8 bytes all ending by a letter (T|Z|U|R|S|Y) and #.

Related

Decoding a .xwd image in Julia

I am trying to write a decoder for a .xwd image (X Window Dump), since ImageMagick is quite slow.
The only specifications I found are:
http://www.opensource.apple.com/source/X11/X11-0.40.80/xc/include/XWDFile.h?txt
https://formats.kaitai.io/xwd/index.html
From which I managed to read the header:
xwd_data = read(`xwd -id $id`)
function get_header(data)
args = [reinterpret(Int32, reverse(data[4*i-3:4*i]))[1] for i in 1:25]
xwd = XwdHeader(args...)
return xwd
end
struct XwdHeader
header_size::Int32
file_version::Int32
pixmap_format::Int32
pixmap_depth::Int32
pixmap_width::Int32
pixmap_height::Int32
xoffset::Int32
byte_order::Int32
bitmap_unit::Int32
bitmap_bit_order::Int32
bitmap_pad::Int32
bits_per_pixel::Int32
bytes_per_line::Int32
visual_class::Int32
red_mask::Int32
green_mask::Int32
blue_mask::Int32
bits_per_rgb::Int32
colormap_entries::Int32
ncolors::Int32
window_width::Int32
window_height::Int32
window_x::Int32
window_y::Int32
window_bdrwidth::Int32
end
and the colormap, which is stored in blocks of 12 bytes and in little-endian byte order:
function read_colormap_entry(n, data, header)
offset = header.header_size + 1
poff = 12*n
px = Pixel(reinterpret(UInt32, reverse(data[offset+poff:offset+poff+3]))[1],
reinterpret(UInt16, reverse(data[offset+poff+4:offset+poff+5]))[1],
reinterpret(UInt16, reverse(data[offset+poff+6:offset+poff+7]))[1],
reinterpret(UInt16, reverse(data[offset+poff+8:offset+poff+9]))[1],
reinterpret(UInt8, data[offset+poff+10])[1],
reinterpret(UInt8, data[offset+poff+11])[1])
println("Pixel number ", px.entry_number >> 16)
println("R ", px.red >> 8)
println("G ", px.green >> 8)
println("B ", px.blue >> 8)
println("flags ", px.flags)
println("padding ",px.padding)
end
struct Pixel
entry_number::UInt32
red::UInt16
green::UInt16
blue::UInt16
flags::UInt8
padding::UInt8
end
julia> read_colormap_entry(0, data, header)
Pixel number 0
R 0
G 0
B 0
flags 7
padding 0
julia> read_colormap_entry(1, data, header)
Pixel number 1
R 1
G 1
B 1
flags 7
padding 0
julia> read_colormap_entry(2, data, header)
Pixel number 2
R 2
G 2
B 2
flags 7
padding 0
Now I have the actual image data stored in 4 byte blocks per pixel in the "Direct Color" visual class. Does anybody know howto extract the RGB values from this ?
edit:
By playing around with the data I found out how to extract the R and G values
function read_pixel(i, j, data, header::XwdHeader)
w = header.window_width
h = header.window_height
offset = header.header_size + header.colormap_entries * 12 + 1
poff = 4*((i-1)*w + (j-1))
px = reinterpret(UInt32, reverse(data[offset+poff:offset+poff+3]))[1]
println("Px value ", px)
r = (px & xwd.red_mask) >> 16
g = (px & xwd.green_mask) >> 8
b = (px & xwd.blue_mask)
println("r ", r)
println("g ", g)
println("b ", b)
end
which gives the correct R and G values, but the B value should be non zero.
julia> read_pixel(31, 31, data, xwd_header)
Px value 741685248
r 53
g 56
b 0
I basically have no idea what I am doing with the color masks and the
bit-shifts. Can anyone explain this ? Thanks !

Infering set mapping in Matlab

I am trying to infer a mapping scheme from set A to B (given below). Is there a way (Toolbox, long-forgotten File Exchange Gem, ...) to do that in Matlab?
My A and B are:
A = [8955573624 8727174542 6144057737 6697647320 1335549467 6669202192...
9276317113 5048034450 4757279524 1423969226 9729294957 4332046813...
0681780168 8231841017 9809242207 5584677643 6193476760 7203972648...
7286156579 5669792887 6789954237 8042954283 7426511939 4053045131...
8629149977 2997522935 9363344270 9890870146 9426932555 5755262458...
8327043690 0162545530 6451719711 5376165082 0595003112 5172323540...
9314878787 6822370777 8236826223 3097377830];
B = [000 001 001 003 003 004...
004 005 005 005 005 007...
007 009 009 009 010 010...
013 013 013 018 018 018...
018 019 019 019 020 020...
020 024 024 024 024 027...
027 027 027 028];
A brute-force method may be a good starting point. It at least give one some place to start thinking about the problem. I include the code I used to find out that for the first four numbers the following order of operations on each of the 10 digits in the gives the 3 digit code.
#mod, #times, #rem, #mod, #times, #plus, #rem, #rem, #mod
However
Elapsed time is 391.706191 seconds.
Code
data = [8955573624 000
8727174542 001
6144057737 001];
operations = {#plus, #minus, #times, #rdivide, #mod, #rem};
tic;
j = 1; % start from 1st row
while true
a = data(j,1);
digits = arrayfun(#str2mat,b(:)); b = num2str(a(1)); % Digits
if j == 1; % Find a set of operations which converts from digits to the code
value = NaN;
trials = 0;
while value ~= data(j,2) || trials > 1e3
ops = datasample(operations,numel(digits)-1); % Random operations
value = digits(1);
for jj = 1:numel(digits)-1
value = arrayfun(ops{jj},value,digits(jj+1));
end
trials = trials + 1;
end
else % Test whether it works for j > 1
value = digits(1);
for jj = 1:numel(digits)-1
value = arrayfun(ops{jj},value,digits(jj+1));
end
end
if value == data(j,2);
if j == size(data,1); break; end;
j = j + 1;
else
j = 1;
end
end
toc;
In terms of other things to try in the framework of this code:
Allowing for the digits to be tested as larger portions of the code. E.g. split the first code into 89,5,55,736,2,4 as opposed to only into single digits
Allowing other/more operations
Paralleling the attempts
Splitting the codes into digits before the while loop (<- Probably the easiest optimization to do here)
Trying the operations on all the codes at once (vectorising)
Changing both code and the answer into binary and trying to find a map there
Hope that helps. Even though It does not straight up solve your problem it might help you think about it in a new way.

CRC Polynomial Division

I am trying to use polynomial division to find the CRC check bits, but I am struggling with the last stage of the calculation.
I am believe the below conversions are correct:
Pattern = 1010
= x^3 + x
Dataword = 9 8 7
= 1001 1000 0111
= x^11 + x^8 + x^7 + x^2 + x + 1
And finally the polynomial long division I am attempted is:
x^8 + x^6 + x^5 + x^3 + x
_______________________________________
x^3 + x | x^11 + x^8 + x^7 + x^2 + x + 1
x^11 + x^9
....
x^4 + x^2 + x + 1
x^4 + x^2
= x + 1
My question is, is the remainder / answer x + 1 or do I take it a step further and remove the x leaving the remainder as just 1?
Thank you for your help!
We can check by mod 2 division (XOR) too, the following code shows a python implementation of CRC checking, we need to follow the steps listed below:
Convert CRC / data polynomials to corresponding binary equivalents.
if the CRC key (binary representation obtained from the polynomial) has k bits, we need to pad an additional k-1 bits with the data to check for errors. In the example given, the bits 011 should be appended to the data, not 0011, since k=4.
At the transmitter end,
The binary data is to be augmented first by adding k-1 zeros in the end of the data.
Use modulo-2 binary division to divide binary data by the CRC key and store remainder of division.
Append the remainder at the end of the data to form the encoded data and send the same
At the receiver end (Check if there are errors introduced in transmission)
Perform modulo-2 division again on the sent data with the CRC key and if the remainder is 0, then there are no errors.
Now let's implement the above:
def CRC_polynomial_to_bin_code(pol):
return bin(eval(pol.replace('^', '**').replace('x','2')))[2:]
def get_remainder(data_bin, gen_bin):
ng = len(gen_bin)
data_bin += '0'*(ng-1)
nd = len(data_bin)
divisor = gen_bin
i = 0
remainder = ''
print('\nmod 2 division steps:')
print('divisor dividend remainder')
while i < nd:
j = i + ng - len(remainder)
if j > nd:
remainder += data_bin[i:]
break
dividend = remainder + data_bin[i:j]
remainder = ''.join(['1' if dividend[k] != gen_bin[k] else '0' for k in range(ng)])
print('{:8s} {:8s} {:8s}'.format(divisor, dividend, remainder[1:]))
remainder = remainder.lstrip('0')
i = j
return remainder.zfill(ng-1)
gen_bin = CRC_polynomial_to_bin_code('x^3+x')
data_bin = CRC_polynomial_to_bin_code('x^11 + x^8 + x^7 + x^2 + x + 1')
print('transmitter end:\n\nCRC key: {}, data: {}'.format(gen_bin, data_bin))
r = get_remainder(data_bin, gen_bin)
data_crc = data_bin + r
print('\nencoded data: {}'.format(data_crc))
print('\nreceiver end:')
r = get_remainder(data_crc, gen_bin)
print('\nremainder {}'.format(r))
if eval(r) == 0:
print('data received at the receiver end has no errors')
# ---------------------------------
# transmitter end:
#
# CRC key: 1010, data: 100110000111
#
# mod 2 division steps:
# divisor dividend remainder
# 1010 1001 011
# 1010 1110 100
# 1010 1000 010
# 1010 1000 010
# 1010 1011 001
# 1010 1100 110
# 1010 1100 110
#
# encoded data: 100110000111110
# ---------------------------------
# receiver end:
#
# mod 2 division steps:
# divisor dividend remainder
# 1010 1001 011
# 1010 1110 100
# 1010 1000 010
# 1010 1000 010
# 1010 1011 001
# 1010 1111 101
# 1010 1010 000
#
# remainder 000
# data received at the receiver end has no errors
# ---------------------------------

JPEG compression implementation in MATLAB

I'm working on an implementation of the JPEG compression algorithm in MATLAB. I've run into some issues when computing the discrete cosine transform(DCT) of the 8x8 image blocks(T = H * F * H_transposed, H is the matrix containing the DCT coefficients of an 8x8 matrix, generated with dctmtx(8) and F is an 8x8 image block). The code is bellow:
jpegCompress.m
function y = jpegCompress(x, quality)
% y = jpegCompress(x, quality) compresses an image X based on 8 x 8 DCT
% transforms, coefficient quantization and Huffman symbol coding. Input
% quality determines the amount of information that is lost and compression achieved. y is the encoding structure containing fields:
% y.size size of x
% y.numblocks number of 8 x 8 encoded blocks
% y.quality quality factor as percent
% y.huffman Huffman coding structure
narginchk(1, 2); % check number of input arguments
if ~ismatrix(x) || ~isreal(x) || ~ isnumeric(x) || ~ isa(x, 'uint8')
error('The input must be a uint8 image.');
end
if nargin < 2
quality = 1; % default value for quality
end
if quality <= 0
error('Input parameter QUALITY must be greater than zero.');
end
m = [16 11 10 16 24 40 51 61 % default JPEG normalizing array
12 12 14 19 26 58 60 55 % and zig-zag reordering pattern
14 13 16 24 40 57 69 56
14 17 22 29 51 87 80 62
18 22 37 56 68 109 103 77
24 35 55 64 81 104 113 92
49 64 78 87 103 121 120 101
72 92 95 98 112 100 103 99] * quality;
order = [1 9 2 3 10 17 25 18 11 4 5 12 19 26 33 ...
41 34 27 20 13 6 7 14 21 28 35 42 49 57 50 ...
43 36 29 22 15 8 16 23 30 37 44 51 58 59 52 ...
45 38 31 24 32 39 46 53 60 61 54 47 40 48 55 ...
62 63 56 64];
[xm, xn] = size(x); % retrieve size of input image
x = double(x) - 128; % level shift input
t = dctmtx(8); % compute 8 x 8 DCT matrix
% Compute DCTs pf 8 x 8 blocks and quantize coefficients
y = blkproc(x, [8 8], 'P1 * x * P2', t, t');
y = blkproc(y, [8 8], 'round(x ./ P1)', m); % <== nearly all elements from y are zero after this step
y = im2col(y, [8 8], 'distinct'); % break 8 x 8 blocks into columns
xb = size(y, 2); % get number of blocks
y = y(order, :); % reorder column elements
eob = max(x(:)) + 1; % create end-of-block symbol
r = zeros(numel(y) + size(y, 2), 1);
count = 0;
for j = 1:xb % process one block(one column) at a time
i = find(y(:, j), 1, 'last'); % find last non-zero element
if isempty(i) % check if there are no non-zero values
i = 0;
end
p = count + 1;
q = p + i;
r(p:q) = [y(1:i, j); eob]; % truncate trailing zeros, add eob
count = count + i + 1; % and add to output vector
end
r((count + 1):end) = []; % delete unused portion of r
y = struct;
y.size = uint16([xm xn]);
y.numblocks = uint16(xb);
y.quality = uint16(quality * 100);
y.huffman = mat2huff(r);
mat2huff is implemented as:
mat2huff.m
function y = mat2huff(x)
%MAT2HUFF Huffman encodes a matrix.
% Y = mat2huff(X) Huffman encodes matrix X using symbol
% probabilities in unit-width histogram bins between X's minimum
% and maximum value s. The encoded data is returned as a structure
% Y :
% Y.code the Huffman - encoded values of X, stored in
% a uint16 vector. The other fields of Y contain
% additional decoding information , including :
% Y.min the minimum value of X plus 32768
% Y.size the size of X
% Y.hist the histogram of X
%
% If X is logical, uintB, uint16 ,uint32 ,intB ,int16, or double,
% with integer values, it can be input directly to MAT2HUF F. The
% minimum value of X must be representable as an int16.
%
% If X is double with non - integer values --- for example, an image
% with values between O and 1 --- first scale X to an appropriate
% integer range before the call.For example, use Y
% MAT2HUFF (255 * X) for 256 gray level encoding.
%
% NOTE : The number of Huffman code words is round(max(X(:)))
% round (min(X(:)))+1. You may need to scale input X to generate
% codes of reasonable length. The maximum row or column dimension
% of X is 65535.
if ~ismatrix(x) || ~isreal(x) || (~isnumeric(x) && ~islogical(x))
error('X must be a 2-D real numeric or logical matrix.');
end
% Store the size of input x.
y.size = uint32(size(x));
% Find the range of x values
% by +32768 as a uint16.
x = round(double(x));
xmin = min(x(:));
xmax = max(x(:));
pmin = double(int16(xmin));
pmin = uint16(pmin+32768);
y.min = pmin;
% Compute the input histogram between xmin and xmax with unit
% width bins , scale to uint16 , and store.
x = x(:)';
h = histc(x, xmin:xmax);
if max(h) > 65535
h = 65535 * h / max(h);
end
h = uint16(h);
y.hist = h;
% Code the input mat rix and store t h e r e s u lt .
map = huffman(double(h)); % Make Huffman code map
hx = map(x(:) - xmin + 1); % Map image
hx = char(hx)'; % Convert to char array
hx = hx(:)';
hx(hx == ' ') = [ ]; % Remove blanks
ysize = ceil(length(hx) / 16); % Compute encoded size
hx16 = repmat('0', 1, ysize * 16); % Pre-allocate modulo-16 vector
hx16(1:length(hx)) = hx; % Make hx modulo-16 in length
hx16 = reshape(hx16, 16, ysize); % Reshape to 16-character words
hx16 = hx16' - '0'; % Convert binary string to decimal
twos = pow2(15 : - 1 : 0);
y.code = uint16(sum(hx16 .* twos(ones(ysize ,1), :), 2))';
Why is the block processing step generating mostly null values?
It is likely that multiplying the Quantization values you have by four is causing the DCT coefficients to go to zero.

Code Golf: Gray Code

Locked. This question and its answers are locked because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions.
The Challenge
The shortest program by character count that outputs the n-bit Gray Code. n will be an arbitrary number smaller than 1000100000 (due to user suggestions) that is taken from standard input. The gray code will be printed in standard output, like in the example.
Note: I don't expect the program to print the gray code in a reasonable time (n=100000 is overkill); I do expect it to start printing though.
Example
Input:
4
Expected Output:
0000
0001
0011
0010
0110
0111
0101
0100
1100
1101
1111
1110
1010
1011
1001
1000
Python - 53 chars
n=1<<input()
for x in range(n):print bin(n+x^x/2)[3:]
This 54 char version overcomes the limitation of range in Python2 so n=100000 works!
x,n=0,1<<input()
while n>x:print bin(n+x^x/2)[3:];x+=1
69 chars
G=lambda n:n and[x+y for x in'01'for y in G(n-1)[::1-2*int(x)]]or['']
75 chars
G=lambda n:n and['0'+x for x in G(n-1)]+['1'+x for x in G(n-1)[::-1]]or['']
APL (29 chars)
With the function F as (⌽ is the 'rotate' char)
z←x F y
z←(0,¨y),1,¨⌽y
This produces the Gray Code with 5 digits (⍴ is now the 'rho' char)
F/5⍴⊂0,1
The number '5' can be changed or be a variable.
(Sorry about the non-printable APL chars. SO won't let me post images as a new user)
Impossible! language (54 58 chars)
#l{'0,'1}1[;#l<][%;~['1%+].>.%['0%+].>.+//%1+]<>%[^].>
Test run:
./impossible gray.i! 5
Impossible v0.1.28
00000
00001
00011
00010
00110
00111
00101
00100
01100
01101
01111
01110
01010
01011
01001
01000
11000
11001
11011
11010
11110
11111
11101
11100
10100
10101
10111
10110
10010
10011
10001
10000
(actually I don't know if personal languages are allowed, since Impossible! is still under development, but I wanted to post it anyway..)
Golfscript - 27 chars
Reads from stdin, writes to stdout
~2\?:),{.2/^)+2base''*1>n}%
Sample run
$ echo 4 | ruby golfscript.rb gray.gs
0000
0001
0011
0010
0110
0111
0101
0100
1100
1101
1111
1110
1010
1011
1001
1000
Ruby - 49 chars
(1<<n=gets.to_i).times{|x|puts"%.#{n}b"%(x^x/2)}
This works for n=100000 with no problem
C++, 168 characters, not including whitespaces:
#include <iostream>
#include <string>
int r;
void x(std::string p, char f=48)
{
if(!r--)std::cout<<p<<'\n';else
{x(p+f);x(p+char(f^1),49);}
r++;
}
int main() {
std::cin>>r;
x("");
return 0;
}
Haskell, 82 characters:
f a=map('0':)a++map('1':)(reverse a)
main=interact$unlines.(iterate f[""]!!).read
Point-free style for teh win! (or at least 4 fewer strokes). Kudos to FUZxxl.
previous: 86 characters:
f a=map('0':)a++map('1':)(reverse a)
main=interact$ \s->unlines$iterate f[""]!!read s
Cut two strokes with interact, one with unlines.
older: 89 characters:
f a=map('0':)a++map('1':)(reverse a)
main=readLn>>= \s->putStr$concat$iterate f["\n"]!!s
Note that the laziness gets you your immediate output for free.
Mathematica 50 Chars
Nest[Join["0"<>#&/##,"1"<>#&/#Reverse##]&,{""},#]&
Thanks to A. Rex for suggestions!
Previous attempts
Here is my attempt in Mathematica (140 characters). I know that it isn't the shortest, but I think it is the easiest to follow if you are familiar with functional programming (though that could be my language bias showing). The addbit function takes an n-bit gray code and returns an n+1 bit gray code using the logic from the wikipedia page.. The make gray code function applies the addbit function in a nested manner to a 1 bit gray code, {{0}, {1}}, until an n-bit version is created. The charactercode function prints just the numbers without the braces and commas that are in the output of the addbit function.
addbit[set_] :=
Join[Map[Prepend[#, 0] &, set], Map[Prepend[#, 1] &, Reverse[set]]]
MakeGray[n_] :=
Map[FromCharacterCode, Nest[addbit, {{0}, {1}}, n - 1] + 48]
Straightforward Python implementation of what's described in Constructing an n-bit Gray code on Wikipedia:
import sys
def _gray(n):
if n == 1:
return [0, 1]
else:
p = _gray(n-1)
pr = [x + (1<<(n-1)) for x in p[::-1]]
return p + pr
n = int(sys.argv[1])
for i in [("0"*n + bin(a)[2:])[-n:] for a in _gray(n)]:
print i
(233 characters)
Test:
$ python gray.py 4
0000
0001
0011
0010
0110
0111
0101
0100
1100
1101
1111
1110
1010
1011
1001
1000
C, 203 Characters
Here's a sacrificial offering, in C:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char s[256];
int b, i, j, m, g;
gets(s);
b = atoi(s);
for (i = 0; i < 1 << b; ++i)
{
g = i ^ (i / 2);
m = 1 << (b - 1);
for (j = 0; j < b; ++j)
{
s[j] = (g & m) ? '1' : '0';
m >>= 1;
}
s[j] = '\0';
puts(s);
}
return 0;
}
C#, 149143 characters
C# isn't the best language for code golf, but I thought I'd go at it anyway.
static void Main(){var s=1L<<int.Parse(Console.ReadLine());for(long i=0;i<s;i++){Console.WriteLine(Convert.ToString(s+i^i/2,2).Substring(1));}}
Readable version:
static void Main()
{
var s = 1L << int.Parse(Console.ReadLine());
for (long i = 0; i < s; i++)
{
Console.WriteLine(Convert.ToString(s + i ^ i / 2, 2).Substring(1));
}
}
And here is my Fantom sacrificial offering
public static Str[]grayCode(Int i){if(i==1)return["0","1"];else{p:=grayCode(i-1);p.addAll(p.dup.reverse);p.each|s,c|{if(c<(p.size/2))p[c]="0"+s;else p[c]="1"+s;};return p}}
(177 char)
Or the expanded version:
public static Str[] grayCode(Int i)
{
if (i==1) return ["0","1"]
else{
p := grayCode(i-1);
p.addAll(p.dup.reverse);
p.each |s,c|
{
if(c<(p.size/2))
{
p[c] = "0" + s
}
else
{
p[c] = "1" + s
}
}
return p
}
}
F#, 152 characters
let m=List.map;;let rec g l=function|1->l|x->g((m((+)"0")l)#(l|>List.rev|>m((+)"1")))(x - 1);;stdin.ReadLine()|>int|>g["0";"1"]|>List.iter(printfn "%s")
F# 180 175 too many characters
This morning I did another version, simplifying the recursive version, but alas due to recursion it wouldn't do the 100000.
Recursive solution:
let rec g m n l =
if(m = n) then l
else List.map ((+)"0") l # List.map ((+)"1") (List.rev(l)) |> g (m+1) n
List.iter (fun x -> printfn "%s" x) (g 1 (int(stdin.ReadLine())) ["0";"1"]);;
After that was done I created a working version for the "100000" requirement - it's too long to compete with the other solutions shown here and I probably re-invented the wheel several times over, but unlike many of the solutions I have seen here it will work with a very,very large number of bits and hey it was a good learning experience for an F# noob - I didn't bother to shorten it, since it's way too long anyway ;-)
Iterative solution: (working with 100000+)
let bits = stdin.ReadLine() |>int
let n = 1I <<< bits
let bitcount (n : bigint) =
let mutable m = n
let mutable c = 1
while m > 1I do
m <- m >>>1
c<-c+1
c
let rec traverseBits m (number: bigint) =
let highbit = bigint(1 <<< m)
if m > bitcount number
then number
else
let lowbit = 1 <<< m-1
if (highbit&&& number) > 0I
then
let newnum = number ^^^ bigint(lowbit)
traverseBits (m+1) newnum
else traverseBits (m+1) number
let res = seq
{
for i in 0I..n do
yield traverseBits 1 i
}
let binary n m = seq
{
for i = m-1 downto 0 do
let bit = bigint(1 <<< i)
if bit &&&n > 0I
then yield "1"
else yield "0"
}
Seq.iter (fun x -> printfn "%s" (Seq.reduce (+) (binary x bits))) res
Lua, 156 chars
This is my throw at it in Lua, as close as I can get it.
LuaJIT (or lua with lua-bitop): 156 bytes
a=io.read()n,w,b=2^a,io.write,bit;for x=0,n-1 do t=b.bxor(n+x,b.rshift(x,1))for k=a-1,0,-1 do w(t%2^k==t%n and 0 or 1)t=t%2^k==t and t or t%2^k end w'\n'end
Lua 5.2: 154 bytes
a=io.read()n,w,b=2^a,io.write,bit32;for x=0,n-1 do t=b.XOR(n+x,b.SHR(x,1))for k=a-1,0,-1 do w(t%2^k==t%n and 0 or 1)t=t%2^k==t and t or t%2^k end w'\n'end
In cut-free Prolog (138 bytes if you remove the space after '<<'; submission editor truncates the last line without it):
b(N,D):-D=0->nl;Q is D-1,H is N>>Q/\1,write(H),b(N,Q).
c(N,D):-N=0;P is N xor(N//2),b(P,D),M is N-1,c(M,D).
:-read(N),X is 1<< N-1,c(X,N).
Ruby, 50 Chars
(2**n=gets.to_i).times{|i|puts"%0#{n}d"%i.to_s(2)}

Resources