Skip to content

Commit d559f77

Browse files
committed
added answer for ch1_q7
1 parent ad16dd5 commit d559f77

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module.exports = Strings_1_7 = (function() {
2+
return {
3+
// Rotates a matrix 90 degrees clockwise
4+
// Solution #7 from the book.
5+
// @param {Array} img - matrix
6+
// @retuns {Boolean} - a rotated a matrix
7+
rotateMatrix: function(img) {
8+
var len = img.length - 1;
9+
for (var i1 = 0; i1 < len; i1++) {
10+
for (var i2 = i1; i2 < len - i1; i2++) {
11+
var increasing = i2;
12+
var decreasing = len - i2;
13+
var fixedMax = len - i1;
14+
var fixedMin = i1;
15+
var tmp = img[decreasing][fixedMin]; //left
16+
img[decreasing][fixedMin] = img[fixedMax][decreasing]; //bottom
17+
img[fixedMax][decreasing] = img[increasing][fixedMax]; //right
18+
img[increasing][fixedMax] = img[fixedMin][increasing]; // top
19+
img[fixedMin][increasing] = tmp;
20+
}
21+
}
22+
return img;
23+
}
24+
};
25+
}());
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
require('../../test_helper');
2+
describe('1.7 #rotateMatrix', function() {
3+
it('returns a rotated matrix', function() {
4+
var data = [
5+
[
6+
[1, 2, 3, 4, 5],
7+
[6, 7, 8, 9, 10],
8+
[11, 12, 13, 14, 15],
9+
[16, 17, 18, 19, 20],
10+
[21, 22, 23, 24, 25]
11+
], [
12+
[21, 16, 11, 6, 1],
13+
[22, 17, 12, 7, 2],
14+
[23, 18, 13, 8, 3],
15+
[24, 19, 14, 9, 4],
16+
[25, 20, 15, 10, 5]
17+
]
18+
];
19+
expect(Strings_1_7.rotateMatrix(data[0])).to.deep.equal(data[1]);
20+
});
21+
});

test/data-structures/chapter-1/1_8_Spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
require('../../test_helper');
2-
describe('1.9 #isRotation', function () {
2+
describe('1.8 #isRotation', function () {
33
var str1, str2;
44
beforeEach(function () {
55
str1 = 'waterbottle';

0 commit comments

Comments
 (0)