Skip to main content
added 6 characters in body; deleted 3 characters in body
Source Link
DarthGizka
  • 4.9k
  • 2
  • 30
  • 44

The snake pattern might be cute but it fails to achieve a rule-conforming assignment for several valid cases (i.e. whenever a run of equal values touches more than one diagonalstraddles diagonals).

However, strangeArrayCopy() does not use the snake pattern (any more?); it always zigs in the bottom-left to top-right direction and never zags, even though it seems to try very hard to disguise what it does.

Ergo there is no correctness problem in the code, only in the stale commentary and the write-up. ;-)

Here's a different take on the copy logic that performs exactly the same assignments. It is more pedestrian but I think it expresses more clearly what happens and I find it easier to understand/verify.

final int N = 6;
int source_index = N * N;  // copying backwards in order to get descending (non-increasing) values

// diagonals that start in the top row (going down and left)
for (int x0 = 0; x0 < N; ++i)
    for (int x = x0, y = 0; x >= 0; --x, ++y)
        matrix[y][x] = arr[--source_index];

// the remaining diagonals all start in the final column
for (int y0 = 1; y0 < N; ++i)
    for (int x = N - 1, y = y0; y < N; --x, ++y)
        matrix[y][x] = arr[--source_index];

The snake pattern might be cute but it fails to achieve a rule-conforming assignment for several valid cases (i.e. whenever a run of values touches more than one diagonal).

However, strangeArrayCopy() does not use the snake pattern (any more?); it always zigs in the bottom-left to top-right direction and never zags, even though it seems to try very hard to disguise what it does.

Ergo there is no correctness problem in the code, only in the stale commentary and the write-up. ;-)

Here's a different take on the copy logic that performs exactly the same assignments. It is more pedestrian but I think it expresses more clearly what happens and I find it easier to understand/verify.

final int N = 6;
int source_index = N * N;  // copying backwards in order to get descending (non-increasing) values

// diagonals that start in the top row (going down and left)
for (int x0 = 0; x0 < N; ++i)
    for (int x = x0, y = 0; x >= 0; --x, ++y)
        matrix[y][x] = arr[--source_index];

// the remaining diagonals all start in the final column
for (int y0 = 1; y0 < N; ++i)
    for (int x = N - 1, y = y0; y < N; --x, ++y)
        matrix[y][x] = arr[--source_index];

The snake pattern might be cute but it fails to achieve a rule-conforming assignment for several valid cases (i.e. whenever a run of equal values touches straddles diagonals).

However, strangeArrayCopy() does not use the snake pattern (any more?); it always zigs in the bottom-left to top-right direction and never zags, even though it seems to try very hard to disguise what it does.

Ergo there is no correctness problem in the code, only in the stale commentary and the write-up. ;-)

Here's a different take on the copy logic that performs exactly the same assignments. It is more pedestrian but I think it expresses more clearly what happens and I find it easier to understand/verify.

final int N = 6;
int source_index = N * N;  // copying backwards in order to get descending (non-increasing) values

// diagonals that start in the top row (going down and left)
for (int x0 = 0; x0 < N; ++i)
    for (int x = x0, y = 0; x >= 0; --x, ++y)
        matrix[y][x] = arr[--source_index];

// the remaining diagonals all start in the final column
for (int y0 = 1; y0 < N; ++i)
    for (int x = N - 1, y = y0; y < N; --x, ++y)
        matrix[y][x] = arr[--source_index];
added 73 characters in body
Source Link
DarthGizka
  • 4.9k
  • 2
  • 30
  • 44

The snake pattern might be cute but it fails to achieve a rule-conforming assignment for several valid cases (i.e. whenever a run of values touches more than one diagonal).

However, strangeArrayCopy() does not use the snake pattern (any more?); it always zigs in the bottom-left to top-right direction and never zags, even though it seems to try very hard to disguise what it does.

Ergo there is no correctness problem in the code, only in the stale commentary and the write-up. ;-)

Here's a different take on the copy logic that performs exactly the same assignments. It is more pedestrian but I think it expresses more clearly what happens and I find it easier to understand/verify.

final int N = 6;
int source_index = N * N;  // copying backwards in order to get descending (non-increasing) values

// diagonals that start in the top row (going down and left)
for (int x0 = 0; x0 < N; ++i)
    for (int x = x0, y = 0; x >= 0; --x, ++y)
        matrix[y][x] = arr[--source_index];

// the remaining diagonals all start in the final column
for (int y0 = 1; y0 < N; ++i)
    for (int x = N - 1, y = y0; y < N; --x, ++y)
        matrix[y][x] = arr[--source_index];

The snake pattern might be cute but it fails to achieve a rule-conforming assignment for several valid cases (i.e. whenever a run of values touches more than one diagonal).

However, strangeArrayCopy() does not use the snake pattern (any more?); it always zigs in the bottom-left to top-right direction and never zags, even though it seems to try very hard to disguise what it does.

Ergo there is no correctness problem in the code, only in the stale commentary and the write-up. ;-)

Here's a different take on the copy logic that performs exactly the same assignments. It is more pedestrian but I think it expresses more clearly what happens and I find it easier to understand/verify.

final int N = 6;
int source_index = N * N;

// diagonals that start in the top row (going down and left)
for (int x0 = 0; x0 < N; ++i)
    for (int x = x0, y = 0; x >= 0; --x, ++y)
        matrix[y][x] = arr[--source_index];

// the remaining diagonals all start in the final column
for (int y0 = 1; y0 < N; ++i)
    for (int x = N - 1, y = y0; y < N; --x, ++y)
        matrix[y][x] = arr[--source_index];

The snake pattern might be cute but it fails to achieve a rule-conforming assignment for several valid cases (i.e. whenever a run of values touches more than one diagonal).

However, strangeArrayCopy() does not use the snake pattern (any more?); it always zigs in the bottom-left to top-right direction and never zags, even though it seems to try very hard to disguise what it does.

Ergo there is no correctness problem in the code, only in the stale commentary and the write-up. ;-)

Here's a different take on the copy logic that performs exactly the same assignments. It is more pedestrian but I think it expresses more clearly what happens and I find it easier to understand/verify.

final int N = 6;
int source_index = N * N;  // copying backwards in order to get descending (non-increasing) values

// diagonals that start in the top row (going down and left)
for (int x0 = 0; x0 < N; ++i)
    for (int x = x0, y = 0; x >= 0; --x, ++y)
        matrix[y][x] = arr[--source_index];

// the remaining diagonals all start in the final column
for (int y0 = 1; y0 < N; ++i)
    for (int x = N - 1, y = y0; y < N; --x, ++y)
        matrix[y][x] = arr[--source_index];
added 118 characters in body; added 5 characters in body; added 1 character in body
Source Link
DarthGizka
  • 4.9k
  • 2
  • 30
  • 44

The snake pattern might be cute but it fails to achieve a rule-conforming assignment for several valid cases (i.e. whenever a run of values touches more than one diagonal).

However, strangeArrayCopy() does not use the snake pattern (any more?); it always zigs in the bottom-left to top-right direction and never zags, even though it seems to try very hard to disguise what it does.

Ergo there is no correctness problem in the code, only in the stale commentary and the write-up. ;-)

Here's a different take on the copy logic that performs exactly the same assignments. It is more pedestrian but I think it expresses more clearly what happens and I find it easier to understand/verify.

final int N = 6;
int source_index = N * N;

// diagonals that start in the top row (going down and left)
for (int ix0 = 0; ix0 < N; ++i)
    for (int x = ix0, y = 0; x >= 0; --x, ++y)
        matrix[y][x] = arr[--source_index]; 

// the remaining diagonals all start in the final column
for (int iy0 = 1; iy0 < N; ++i)
    for (int x = N - 1, y = i;y0; y < N; --x, ++y)
        matrix[y][x] = arr[--source_index];

The snake pattern might be cute but it fails to achieve a rule-conforming assignment for several valid cases (i.e. whenever a run of values touches more than one diagonal).

However, strangeArrayCopy() does not use the snake pattern (any more?); it always zigs in the bottom-left to top-right direction and never zags, even though it seems to try very hard to disguise what it does.

Ergo there is no correctness problem in the code, only in the stale commentary and the write-up. ;-)

Here's a different take on the copy logic that performs exactly the same assignments. It is more pedestrian but I think it expresses more clearly what happens and I find it easier to understand/verify.

final int N = 6;
int source_index = N * N;

for (int i = 0; i < N; ++i)
    for (int x = i, y = 0; x >= 0; --x, ++y)
        matrix[y][x] = arr[--source_index];

for (int i = 1; i < N; ++i)
    for (int x = N - 1, y = i; y < N; --x, ++y)
        matrix[y][x] = arr[--source_index];

The snake pattern might be cute but it fails to achieve a rule-conforming assignment for several valid cases (i.e. whenever a run of values touches more than one diagonal).

However, strangeArrayCopy() does not use the snake pattern (any more?); it always zigs in the bottom-left to top-right direction and never zags, even though it seems to try very hard to disguise what it does.

Ergo there is no correctness problem in the code, only in the stale commentary and the write-up. ;-)

Here's a different take on the copy logic that performs exactly the same assignments. It is more pedestrian but I think it expresses more clearly what happens and I find it easier to understand/verify.

final int N = 6;
int source_index = N * N;

// diagonals that start in the top row (going down and left)
for (int x0 = 0; x0 < N; ++i)
    for (int x = x0, y = 0; x >= 0; --x, ++y)
        matrix[y][x] = arr[--source_index]; 

// the remaining diagonals all start in the final column
for (int y0 = 1; y0 < N; ++i)
    for (int x = N - 1, y = y0; y < N; --x, ++y)
        matrix[y][x] = arr[--source_index];
added 464 characters in body; added 17 characters in body
Source Link
DarthGizka
  • 4.9k
  • 2
  • 30
  • 44
Loading
added 12 characters in body
Source Link
DarthGizka
  • 4.9k
  • 2
  • 30
  • 44
Loading
Source Link
DarthGizka
  • 4.9k
  • 2
  • 30
  • 44
Loading