Skip to content

Commit 8110d70

Browse files
committed
Add day 26
1 parent 3279681 commit 8110d70

File tree

5 files changed

+285
-0
lines changed

5 files changed

+285
-0
lines changed

Diff for: day26/JavaScript/spiralCopy.js

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/* ==================================== */
2+
/* ===== Matrix Spiral Copy In JS ===== */
3+
/* ==================================== */
4+
5+
function spiralCopy(inputMatrix) {
6+
// your code goes here
7+
let outputArray = [],
8+
numRow = inputMatrix.length,
9+
numCol = inputMatrix[0].length;
10+
11+
let topRow = 0,
12+
btmRow = numRow - 1,
13+
leftCol = 0,
14+
rightCol = numCol - 1;
15+
16+
while ((topRow <= btmRow) && (leftCol <= rightCol)) {
17+
for (let i=leftCol; i<=rightCol; i++) {
18+
outputArray.push(inputMatrix[topRow][i]);
19+
}
20+
topRow++;
21+
22+
for (let i=topRow; i<=btmRow; i++) {
23+
outputArray.push(inputMatrix[i][rightCol]);
24+
}
25+
rightCol--;
26+
27+
if (topRow <= btmRow) {
28+
for (let i=rightCol; i>=leftCol; i--) {
29+
outputArray.push(inputMatrix[btmRow][i]);
30+
}
31+
btmRow--;
32+
}
33+
34+
if (leftCol <= rightCol) {
35+
for (let i=btmRow; i>=topRow; i--) {
36+
outputArray.push(inputMatrix[i][leftCol]);
37+
}
38+
leftCol++;
39+
}
40+
}
41+
42+
return outputArray;
43+
}
44+
45+
console.log(spiralCopy([ [1, 2, 3, 4],
46+
[6, 7, 8, 9],
47+
[11, 12, 13, 14],
48+
[16, 17, 18, 19] ]));
49+
// Should print [ 1, 2, 3, 4, 9, 14, 19, 18, 17, 16, 11, 6, 7, 8, 13, 12 ]
50+
51+
console.log(spiralCopy([ [1, 2, 3, 4, 5],
52+
[6, 7, 8, 9, 10],
53+
[11, 12, 13, 14, 15],
54+
[16, 17, 18, 19, 20] ]));
55+
// Should print [ 1, 2, 3, 4, 5, 10, 15, 20, 19, 18, 17, 16, 11, 6, 7, 8, 9, 14, 13, 12 ]

Diff for: day26/JavaScript/spiralGeneration.js

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* Spiral Matrix Generation
3+
* @author MadhavBahlMD
4+
* @date 24/01/2019
5+
*/
6+
7+
function spiralGeneration (num) {
8+
let spiral = [],
9+
topRow = 0,
10+
btmRow = num-1,
11+
leftCol = 0,
12+
rightCol = num-1,
13+
count = 1;
14+
15+
// Initialize the spiral matrix with zeros
16+
for (let i=0; i<num; i++) {
17+
let thisRow = [];
18+
for (let j=0; j<num; j++)
19+
thisRow.push (0);
20+
spiral.push (thisRow);
21+
}
22+
23+
// Populate the matrix
24+
while ((topRow<=btmRow) && (leftCol<=rightCol)) {
25+
// Populate the top row
26+
for (let i=leftCol; i<=rightCol; i++)
27+
spiral[topRow][i] = count++;
28+
topRow++;
29+
30+
// Populate the right column
31+
for (let i=topRow; i<=btmRow; i++)
32+
spiral[i][rightCol] = count++;
33+
rightCol--;
34+
35+
// Populate the bottom row IF toprow has not become greater than bottom row
36+
if (topRow <= btmRow) {
37+
for (let i=rightCol; i>=leftCol; i--)
38+
spiral[btmRow][i] = count++;
39+
btmRow--;
40+
}
41+
42+
// Populate the left column IF rightCol has not become less than left Col
43+
if (rightCol >= leftCol) {
44+
for (let i=btmRow; i>=topRow; i--)
45+
spiral[i][leftCol] = count++;
46+
leftCol++;
47+
}
48+
}
49+
50+
// Print the result
51+
console.log (spiral);
52+
return spiral;
53+
}
54+
55+
spiralGeneration (2);
56+
spiralGeneration (3);
57+
spiralGeneration (4);
58+
spiralGeneration (5);

Diff for: day26/README.md

+172
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
![cover](./cover.png)
2+
3+
# Day 26 - Array Series Part 9: Spiral Matrix Generation and Matrix Spiral Copy
4+
5+
## Question 1 - Spiral Matrix Generation (1D to 2D)
6+
7+
**Question 1** -- Write a function that accepts an integer N and generates an NxN spiral matrix
8+
9+
**Example**
10+
11+
```
12+
input: N=2
13+
output:
14+
[
15+
[1, 2],
16+
[4, 3]
17+
]
18+
19+
input: N=3
20+
output:
21+
[
22+
[1, 2, 3],
23+
[8, 9, 4],
24+
[7, 6, 5]
25+
]
26+
```
27+
28+
## Question 2 - Matrix Spiral Copy (2D to 1D)
29+
30+
Given a 2D array (matrix) inputMatrix of integers, create a function spiralCopy that copies inputMatrix’s values into a 1D array in a spiral order, clockwise. Your function then should return that array.
31+
32+
**Example**
33+
34+
```js
35+
input: inputMatrix = [ [1, 2, 3, 4, 5],
36+
[6, 7, 8, 9, 10],
37+
[11, 12, 13, 14, 15],
38+
[16, 17, 18, 19, 20] ]
39+
40+
output: [1, 2, 3, 4, 5, 10, 15, 20, 19, 18, 17, 16, 11, 6, 7, 8, 9, 14, 13, 12]
41+
```
42+
43+
![ques](./ques.js)
44+
45+
## Solution to problem 1 - Spiral Matrix Generation
46+
47+
## JavaScript Implementation
48+
49+
### [Solution](./JavaScript/spiralGeneration.js)
50+
51+
```js
52+
/**
53+
* Spiral Matrix Generation
54+
* @author MadhavBahlMD
55+
* @date 24/01/2019
56+
*/
57+
58+
function spiralGeneration (num) {
59+
let spiral = [],
60+
topRow = 0,
61+
btmRow = num-1,
62+
leftCol = 0,
63+
rightCol = num-1,
64+
count = 1;
65+
66+
// Initialize the spiral matrix with zeros
67+
for (let i=0; i<num; i++) {
68+
let thisRow = [];
69+
for (let j=0; j<num; j++)
70+
thisRow.push (0);
71+
spiral.push (thisRow);
72+
}
73+
74+
// Populate the matrix
75+
while ((topRow<=btmRow) && (leftCol<=rightCol)) {
76+
// Populate the top row
77+
for (let i=leftCol; i<=rightCol; i++)
78+
spiral[topRow][i] = count++;
79+
topRow++;
80+
81+
// Populate the right column
82+
for (let i=topRow; i<=btmRow; i++)
83+
spiral[i][rightCol] = count++;
84+
rightCol--;
85+
86+
// Populate the bottom row IF toprow has not become greater than bottom row
87+
if (topRow <= btmRow) {
88+
for (let i=rightCol; i>=leftCol; i--)
89+
spiral[btmRow][i] = count++;
90+
btmRow--;
91+
}
92+
93+
// Populate the left column IF rightCol has not become less than left Col
94+
if (rightCol >= leftCol) {
95+
for (let i=btmRow; i>=topRow; i--)
96+
spiral[i][leftCol] = count++;
97+
leftCol++;
98+
}
99+
}
100+
101+
// Print the result
102+
console.log (spiral);
103+
return spiral;
104+
}
105+
106+
spiralGeneration (2);
107+
spiralGeneration (3);
108+
spiralGeneration (4);
109+
spiralGeneration (5);
110+
```
111+
112+
***
113+
114+
## Solution to problem 2 - Matrix Spiral Copy
115+
116+
## JavaScript Implementation
117+
118+
### [Solution](./JavaScript/spiralCopy.js)
119+
120+
```js
121+
function spiralCopy(inputMatrix) {
122+
// your code goes here
123+
let outputArray = [],
124+
numRow = inputMatrix.length,
125+
numCol = inputMatrix[0].length;
126+
127+
let topRow = 0,
128+
btmRow = numRow - 1,
129+
leftCol = 0,
130+
rightCol = numCol - 1;
131+
132+
while ((topRow <= btmRow) && (leftCol <= rightCol)) {
133+
for (let i=leftCol; i<=rightCol; i++) {
134+
outputArray.push(inputMatrix[topRow][i]);
135+
}
136+
topRow++;
137+
138+
for (let i=topRow; i<=btmRow; i++) {
139+
outputArray.push(inputMatrix[i][rightCol]);
140+
}
141+
rightCol--;
142+
143+
if (topRow <= btmRow) {
144+
for (let i=rightCol; i>=leftCol; i--) {
145+
outputArray.push(inputMatrix[btmRow][i]);
146+
}
147+
btmRow--;
148+
}
149+
150+
if (leftCol <= rightCol) {
151+
for (let i=btmRow; i>=topRow; i--) {
152+
outputArray.push(inputMatrix[i][leftCol]);
153+
}
154+
leftCol++;
155+
}
156+
}
157+
158+
return outputArray;
159+
}
160+
161+
console.log(spiralCopy([ [1, 2, 3, 4],
162+
[6, 7, 8, 9],
163+
[11, 12, 13, 14],
164+
[16, 17, 18, 19] ]));
165+
// Should print [ 1, 2, 3, 4, 9, 14, 19, 18, 17, 16, 11, 6, 7, 8, 13, 12 ]
166+
167+
console.log(spiralCopy([ [1, 2, 3, 4, 5],
168+
[6, 7, 8, 9, 10],
169+
[11, 12, 13, 14, 15],
170+
[16, 17, 18, 19, 20] ]));
171+
// Should print [ 1, 2, 3, 4, 5, 10, 15, 20, 19, 18, 17, 16, 11, 6, 7, 8, 9, 14, 13, 12 ]
172+
```

Diff for: day26/cover.png

154 KB
Loading

Diff for: day26/ques.png

1.39 MB
Loading

0 commit comments

Comments
 (0)