Skip to content

Commit 14bff41

Browse files
committed
Added medium/matrix_spiral
1 parent 4b4c559 commit 14bff41

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed

medium/matrix_spiral.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/**
2+
* Using the JavaScript language, have the function matrixSpiral(strArr) read
3+
* the array of strings stored in strArr which will represent a 2D N matrix, and
4+
* your program should return the elements after printing them in a clockwise,
5+
* spiral order. You should return the newly formed list of elements as a string
6+
* with the numbers separated by commas. For example: if strArr is "[1, 2, 3]",
7+
* "[4, 5, 6]", "[7, 8, 9]" then this looks like the following 2D matrix:
8+
*
9+
* 1 2 3
10+
* 4 5 6
11+
* 7 8 9
12+
*
13+
* So your program should return the elements of this matrix in a clockwise,
14+
* spiral order which is: 1,2,3,6,9,8,7,4,5
15+
*
16+
* https://www.coderbyte.com/results/bhanson:Matrix%20Spiral:JavaScript
17+
*
18+
* @param {array} strArr
19+
* @return {string}
20+
*/
21+
function matrixSpiral(strArr) {
22+
const matrix = strArr.map(JSON.parse);
23+
24+
const results = [];
25+
26+
for (const element of matrixSpiralGenerator(matrix)) {
27+
results.push(element);
28+
}
29+
30+
return results.join(',');
31+
}
32+
33+
function* matrixSpiralGenerator(matrix) {
34+
// RIGHT 0 -> DOWN 1 -> LEFT 2 -> UP 3
35+
const DIRECTIONS = [
36+
[1, 0], // RIGHT
37+
[0, 1], // DOWN
38+
[-1, 0], // LEFT
39+
[0, -1] // UP
40+
];
41+
42+
const visited = Array(matrix.length)
43+
.fill(0)
44+
.map(row => Array(matrix[0].length).fill(0));
45+
46+
const totalElements = matrix.length * matrix[0].length;
47+
48+
let posX = 0;
49+
let posY = 0;
50+
51+
let vector = 0; // index of DIRECTIONS
52+
53+
yield matrix[posY][posX];
54+
visited[posY][posX] = 1;
55+
56+
for (let i = 1; i < totalElements; i++) {
57+
for (let j = 0; j < DIRECTIONS.length; j++) {
58+
let testX = posX + DIRECTIONS[vector][0];
59+
let testY = posY + DIRECTIONS[vector][1];
60+
61+
if (
62+
testX < visited[0].length &&
63+
testY < visited.length &&
64+
visited[testY][testX] === 0
65+
) {
66+
// Good!
67+
posX = testX;
68+
posY = testY;
69+
visited[posY][posX] = 1;
70+
break;
71+
}
72+
// Try next direction
73+
vector = (vector + 1) % 4;
74+
}
75+
76+
yield matrix[posY][posX];
77+
}
78+
}
79+
80+
module.exports = matrixSpiral;

medium/matrix_spiral.test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const matrixSpiral = require('./matrix_spiral');
2+
3+
describe('matrixSpiral()', () => {
4+
test('returns correct matrix spiral', () => {
5+
expect(matrixSpiral(['[1, 2, 3]', '[4, 5, 6]', '[7, 8, 9]'])).toBe(
6+
'1,2,3,6,9,8,7,4,5'
7+
);
8+
9+
expect(matrixSpiral(['[1, 2]', '[10, 14]'])).toBe('1,2,14,10');
10+
11+
expect(
12+
matrixSpiral(['[4, 5, 6, 5]', '[1, 1, 2, 2]', '[5, 4, 2, 9]'])
13+
).toBe('4,5,6,5,2,9,2,4,5,1,1,2');
14+
});
15+
16+
test('passes Coderbyte.com tests', () => {
17+
expect(
18+
matrixSpiral([
19+
'[1, 2, 3, 4, 5]',
20+
'[4, 5, 6, 7, 8]',
21+
'[7, 8, 9, 10, 11]',
22+
'[14, 3, 2, 1, 3]',
23+
'[6, 7, 3, 2, 1]'
24+
])
25+
).toBe('1,2,3,4,5,8,11,3,1,2,3,7,6,14,7,4,5,6,7,10,1,2,3,8,9');
26+
});
27+
});

0 commit comments

Comments
 (0)