|
| 1 | + |
| 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 | + |
| 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 | +``` |
0 commit comments