|
| 1 | + |
| 2 | + |
| 3 | +# Day 20 - Array Partition |
| 4 | + |
| 5 | +Write a program to divide the given array into sub arrays where each sub array is of the length equal to the given partition size. |
| 6 | + |
| 7 | +## Example |
| 8 | + |
| 9 | +partition([1,2,3,4,5,6,7,8], 2) --> [[1, 2], [3, 4], [5, 6], [7, 8]] |
| 10 | +partition([1,2,3,4,5,6,7], 2) --> [[1, 2], [3, 4], [5, 6], [7]] |
| 11 | + |
| 12 | +## JavaScript Solution(s) |
| 13 | + |
| 14 | +### [Solution 1](./JavaScript/sol1.js) |
| 15 | + |
| 16 | +A very simple solution, all we have to is loop through i=0 to array length. In each iteration we check whether current iteration (i) is divisible by size or not, if it is not divisible, we add the current element in `toBeAdded` array otherwise we push the `toBeAdded` array into `partitionedArray`. Also, we check that `toBeAdded` is not wmpty otherwise it will push an empty array to `partitionedArray` |
| 17 | + |
| 18 | + |
| 19 | +```js |
| 20 | +function partition (array, size) { |
| 21 | + let partitionedArray = [], |
| 22 | + toBeAdded = []; |
| 23 | + |
| 24 | + for (let i=0; i<array.length; i++) { |
| 25 | + if ((i)%size === 0) { |
| 26 | + if (toBeAdded.length > 0) partitionedArray.push (toBeAdded); |
| 27 | + toBeAdded = []; |
| 28 | + } |
| 29 | + toBeAdded.push (array[i]); |
| 30 | + } |
| 31 | + if (toBeAdded.length > 0) partitionedArray.push (toBeAdded); |
| 32 | + return partitionedArray; |
| 33 | +} |
| 34 | +``` |
| 35 | + |
| 36 | +### [Solution 2](./JavaScript/sol2.js) |
| 37 | + |
| 38 | +The second solution is also similar to the first one. Here, we create an empty array to hold partitions (let's call the array `partitionedArray`). Now for each element in the "original" array, we retrieve the last element in partitionedArray . If the last element does not exist (in case of empty array), or if it's size is equal to the partition size, we push a new partition array into the `partitionedArray` with the current element. Otherwise, we add the current element into the partition. |
| 39 | + |
| 40 | +```js |
| 41 | +function partition (array, size) { |
| 42 | + let partitionedArray = []; |
| 43 | + |
| 44 | + for (let element of array) { |
| 45 | + let partition = partitionedArray[partitionedArray.length - 1]; |
| 46 | + if (!partition || partition.length === size) { |
| 47 | + partitionedArray.push ([element]); |
| 48 | + } else { |
| 49 | + lastEle.push (element); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + return partitionedArray; |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +### [Solution 3](./JavaScript/sol3.js) |
| 58 | + |
| 59 | +This method will use JavaScript's `slice` method. This is probably the easiest method due to the inbuilt function `slice`. |
| 60 | + |
| 61 | +#### How slice works |
| 62 | + |
| 63 | +array.sllice (startIndex, endIndex); will return a subarray of array including elements from startIndex to endIndex (**not including endIndex**). |
| 64 | + |
| 65 | +#### Steps |
| 66 | + |
| 67 | +- Create an empty `partitionedArray` array |
| 68 | +- run a while loop from index i=0 to less than "original" array.length |
| 69 | +- Push a `slice` of length `size` from `array` to `partitionedArray`. |
| 70 | +- `index = index+size` |
| 71 | + |
| 72 | +```js |
| 73 | +function partition (array, size) { |
| 74 | + let partitionedArray = [], i=0; |
| 75 | + while (i<array.length) { |
| 76 | + partitionedArray.push (array.slice(i, i+size)); |
| 77 | + i += size; |
| 78 | + } |
| 79 | + return partitionedArray; |
| 80 | +} |
| 81 | +``` |
0 commit comments