File tree Expand file tree Collapse file tree 3 files changed +47
-1
lines changed
lib/data-structures/chapter-1
test/data-structures/chapter-1 Expand file tree Collapse file tree 3 files changed +47
-1
lines changed Original file line number Diff line number Diff line change 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+ } ( ) ) ;
Original file line number Diff line number Diff line change 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+ } ) ;
Original file line number Diff line number Diff line change 11require ( '../../test_helper' ) ;
2- describe ( '1.9 #isRotation' , function ( ) {
2+ describe ( '1.8 #isRotation' , function ( ) {
33 var str1 , str2 ;
44 beforeEach ( function ( ) {
55 str1 = 'waterbottle' ;
You can’t perform that action at this time.
0 commit comments