2018-09-30 23:01:58 +01:00
|
|
|
---
|
|
|
|
title: Zig-zag matrix
|
|
|
|
id: 594810f028c0303b75339ad8
|
|
|
|
challengeType: 5
|
|
|
|
---
|
|
|
|
|
|
|
|
## Description
|
|
|
|
<section id='description'>
|
2018-10-20 21:02:47 +03:00
|
|
|
A ''zig-zag'' array is a square arrangement of the first
|
2018-09-30 23:01:58 +01:00
|
|
|
$N^2$ integers, where the
|
|
|
|
numbers increase sequentially as you zig-zag along the array's
|
|
|
|
<a href="https://en.wiktionary.org/wiki/antidiagonal">anti-diagonals</a>.
|
2018-10-20 21:02:47 +03:00
|
|
|
For example, given '''5''', produce this array:
|
2018-09-30 23:01:58 +01:00
|
|
|
<pre>
|
|
|
|
0 1 5 6 14
|
|
|
|
2 4 7 13 15
|
|
|
|
3 8 12 16 21
|
|
|
|
9 11 17 20 22
|
|
|
|
10 18 19 23 24
|
|
|
|
</pre>
|
|
|
|
Write a function that takes the size of the zig-zag matrix, and returns the
|
|
|
|
corresponding matrix as two-dimensional array.
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Instructions
|
|
|
|
<section id='instructions'>
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Tests
|
|
|
|
<section id='tests'>
|
|
|
|
|
|
|
|
```yml
|
2018-10-04 14:37:37 +01:00
|
|
|
tests:
|
|
|
|
- text: ZigZagMatrix must be a function
|
2018-10-20 21:02:47 +03:00
|
|
|
testString: assert.equal(typeof ZigZagMatrix, 'function', 'ZigZagMatrix must be a function');
|
2018-10-04 14:37:37 +01:00
|
|
|
- text: ZigZagMatrix should return array
|
2018-10-20 21:02:47 +03:00
|
|
|
testString: assert.equal(typeof ZigZagMatrix(1), 'object', 'ZigZagMatrix should return array');
|
2018-10-04 14:37:37 +01:00
|
|
|
- text: ZigZagMatrix should return an array of nestes arrays
|
2018-10-20 21:02:47 +03:00
|
|
|
testString: assert.equal(typeof ZigZagMatrix(1)[0], 'object', 'ZigZagMatrix should return an array of nestes arrays');
|
|
|
|
- text: ZigZagMatrix(1) should return [[0]]
|
|
|
|
testString: assert.deepEqual(ZigZagMatrix(1), zm1, 'ZigZagMatrix(1) should return [[0]]');
|
|
|
|
- text: ZigZagMatrix(2) should return [[0, 1], [2, 3]]
|
|
|
|
testString: assert.deepEqual(ZigZagMatrix(2), zm2, 'ZigZagMatrix(2) should return [[0, 1], [2, 3]]');
|
2018-10-04 14:37:37 +01:00
|
|
|
- text: ZigZagMatrix(5) must return specified matrix
|
2018-10-20 21:02:47 +03:00
|
|
|
testString: assert.deepEqual(ZigZagMatrix(5), zm5, 'ZigZagMatrix(5) must return specified matrix');
|
2018-09-30 23:01:58 +01:00
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Challenge Seed
|
|
|
|
<section id='challengeSeed'>
|
|
|
|
|
|
|
|
<div id='js-seed'>
|
|
|
|
|
|
|
|
```js
|
|
|
|
function ZigZagMatrix(n) {
|
|
|
|
// Good luck!
|
|
|
|
return [[], []];
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
### After Test
|
|
|
|
<div id='js-teardown'>
|
|
|
|
|
|
|
|
```js
|
2018-10-20 21:02:47 +03:00
|
|
|
const zm1 = [[0]];
|
|
|
|
const zm2 = [[0, 1], [2, 3]];
|
|
|
|
const zm5 = [
|
|
|
|
[0, 1, 5, 6, 14],
|
|
|
|
[2, 4, 7, 13, 15],
|
|
|
|
[3, 8, 12, 16, 21],
|
|
|
|
[9, 11, 17, 20, 22],
|
|
|
|
[10, 18, 19, 23, 24]
|
|
|
|
];
|
2018-09-30 23:01:58 +01:00
|
|
|
```
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Solution
|
|
|
|
<section id='solution'>
|
|
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
function ZigZagMatrix(n) {
|
|
|
|
const mtx = [];
|
|
|
|
for (let i = 0; i < n; i++) {
|
|
|
|
mtx[i] = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
let i = 1;
|
|
|
|
let j = 1;
|
|
|
|
for (let e = 0; e < n * n; e++) {
|
|
|
|
mtx[i - 1][j - 1] = e;
|
|
|
|
if ((i + j) % 2 === 0) {
|
|
|
|
// Even stripes
|
|
|
|
if (j < n) j++;
|
|
|
|
else i += 2;
|
|
|
|
if (i > 1) i--;
|
|
|
|
} else {
|
|
|
|
// Odd stripes
|
|
|
|
if (i < n) i++;
|
|
|
|
else j += 2;
|
|
|
|
if (j > 1) j--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return mtx;
|
|
|
|
}
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|