Skip to content

Commit 7e5d937

Browse files
committed
Add day 25
1 parent 45e3c40 commit 7e5d937

File tree

5 files changed

+177
-0
lines changed

5 files changed

+177
-0
lines changed

day25/JavaScript/rotateTile.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* @author MadhavBahlMD
3+
* @date 23/01/2019
4+
* Method -- Creating a new matrix to represent the rotated tile
5+
*/
6+
7+
function rotateTile (arr) {
8+
let n = arr.length;
9+
10+
// print the original tile
11+
console.log ('Original Tile: ');
12+
let toPrint = '';
13+
for (let array of arr) {
14+
toPrint = '';
15+
for (let element of array) {
16+
toPrint += element + ' ';
17+
}
18+
console.log (toPrint);
19+
}
20+
21+
// Make another tile to store the rotated tile
22+
let rotatedTile = [];
23+
// Initialize with zeros
24+
for (let i=0; i<n; i++) {
25+
let row = [];
26+
for (let j=0; j<n; j++) {
27+
row.push(0);
28+
}
29+
rotatedTile.push (row);
30+
}
31+
32+
for (let i=0; i<n; i++) {
33+
for (let j=0; j<n; j++)
34+
rotatedTile [i][j] = arr[(n-j)-1][i];
35+
}
36+
37+
// print the rotated tile
38+
console.log ('Rotated Tile: ');
39+
for (let array of rotatedTile) {
40+
toPrint = '';
41+
for (let element of array) {
42+
toPrint += element + ' ';
43+
}
44+
console.log (toPrint);
45+
}
46+
47+
return rotatedTile;
48+
}
49+
50+
rotateTile ([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);

day25/JavaScript/rotateTile2.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// /**
2+
// * @author MadhavBahlMD
3+
// * @date 23/01/2019
4+
// * Method -- Changing the existing tile
5+
// */
6+
7+
// function rotateTile (arr) {
8+
// let n = arr.length;
9+
10+
// // print the original tile
11+
// console.log ('Original Tile: ');
12+
// let toPrint = '';
13+
// for (let array of arr) {
14+
// toPrint = '';
15+
// for (let element of array) {
16+
// toPrint += element + ' ';
17+
// }
18+
// console.log (toPrint);
19+
// }
20+
21+
22+
23+
// return arr;
24+
// }
25+
26+
// rotateTile ([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);

day25/README.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
![cover](./cover.png)
2+
3+
# Day 25 - Array Series Part 8: Rotate Square Tile
4+
5+
**Question** -- Given a 2 dimensional (NxN) square array, write a function that rotates the given array by 90 degrees clockwise
6+
7+
**Example**
8+
9+
```
10+
input:
11+
[
12+
[1, 2, 3],
13+
[4, 5, 6],
14+
[7, 8, 9]
15+
]
16+
17+
output:
18+
[
19+
[7, 4, 1],
20+
[8, 5, 2],
21+
[9, 6, 3]
22+
]
23+
```
24+
25+
![ques](./ques.png)
26+
27+
## Solution
28+
29+
## JavaScript Implementation
30+
31+
### [Solution 1 -- Creating a new matrix for rotated tile](./JavaScript/rotateTile.js)
32+
33+
#### Main Logic:
34+
35+
```js
36+
for (let i=0; i<n; i++) {
37+
for (let j=0; j<n; j++)
38+
rotatedTile [i][j] = arr[(n-j)-1][i];
39+
}
40+
```
41+
42+
#### Complete Code:
43+
44+
```js
45+
/**
46+
* @author MadhavBahlMD
47+
* @date 23/01/2019
48+
* Method -- Creating a new matrix to represent the rotated tile
49+
*/
50+
51+
function rotateTile (arr) {
52+
let n = arr.length;
53+
54+
// print the original tile
55+
console.log ('Original Tile: ');
56+
let toPrint = '';
57+
for (let array of arr) {
58+
toPrint = '';
59+
for (let element of array) {
60+
toPrint += element + ' ';
61+
}
62+
console.log (toPrint);
63+
}
64+
65+
// Make another tile to store the rotated tile
66+
let rotatedTile = [];
67+
// Initialize with zeros
68+
for (let i=0; i<n; i++) {
69+
let row = [];
70+
for (let j=0; j<n; j++) {
71+
row.push(0);
72+
}
73+
rotatedTile.push (row);
74+
}
75+
76+
for (let i=0; i<n; i++) {
77+
for (let j=0; j<n; j++)
78+
rotatedTile [i][j] = arr[(n-j)-1][i];
79+
}
80+
81+
// print the rotated tile
82+
console.log ('Rotated Tile: ');
83+
for (let array of rotatedTile) {
84+
toPrint = '';
85+
for (let element of array) {
86+
toPrint += element + ' ';
87+
}
88+
console.log (toPrint);
89+
}
90+
91+
return rotatedTile;
92+
}
93+
94+
rotateTile ([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
95+
```
96+
97+
### [Solution 2 -- Changing the original tile](./JavaScript/rotateTile2.js)
98+
99+
```js
100+
To be added
101+
```

day25/cover.png

138 KB
Loading

day25/ques.png

968 KB
Loading

0 commit comments

Comments
 (0)