-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ef1a7b4
commit 88e5a92
Showing
7 changed files
with
190 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* @author MadhavBahlMD | ||
* @date 16/01/2019 | ||
*/ | ||
|
||
/* ========================================= */ | ||
/* ===== Array Partition Problem In JS ===== */ | ||
/* ========================================= */ | ||
|
||
function partition (array, size) { | ||
let partitionedArray = [], | ||
toBeAdded = []; | ||
|
||
for (let i=0; i<array.length; i++) { | ||
if ((i)%size === 0) { | ||
if (toBeAdded.length > 0) partitionedArray.push (toBeAdded); | ||
toBeAdded = []; | ||
} | ||
toBeAdded.push (array[i]); | ||
} | ||
if (toBeAdded.length > 0) partitionedArray.push (toBeAdded); | ||
return partitionedArray; | ||
} | ||
|
||
// Test our partition function | ||
console.log(partition ([1,2,3,4,5,6,7,8], 2)); | ||
console.log(partition ([1,2,3,4,5,6,7], 2)); | ||
console.log(partition ([1,2,5,3,4,6,7,1,2,4,6,4,5], 3)); | ||
console.log(partition ([1,2,5,3,4,6,7,1,2,4,6,4,5], 4)); | ||
|
||
/** | ||
* Required Output | ||
* [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ], [ 7, 8 ] ] | ||
* [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ], [ 7 ] ] | ||
* [ [ 1, 2, 5 ], [ 3, 4, 6 ], [ 7, 1, 2 ], [ 4, 6, 4 ], [ 5 ] ] | ||
* [ [ 1, 2, 5, 3 ], [ 4, 6, 7, 1 ], [ 2, 4, 6, 4 ], [ 5 ] ] | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* @author MadhavBahlMD | ||
* @date 16/01/2019 | ||
*/ | ||
|
||
/* ========================================= */ | ||
/* ===== Array Partition Problem In JS ===== */ | ||
/* ========================================= */ | ||
|
||
function partition (array, size) { | ||
let partitionedArray = []; | ||
|
||
for (let element of array) { | ||
let partition = partitionedArray[partitionedArray.length - 1]; | ||
|
||
if (!partition || partition.length === size) { | ||
partitionedArray.push ([element]); | ||
} else { | ||
lastEle.push (element); | ||
} | ||
} | ||
|
||
return partitionedArray; | ||
} | ||
|
||
// Test our partition function | ||
console.log(partition ([1,2,3,4,5,6,7,8], 2)); | ||
console.log(partition ([1,2,3,4,5,6,7], 2)); | ||
console.log(partition ([1,2,5,3,4,6,7,1,2,4,6,4,5], 3)); | ||
console.log(partition ([1,2,5,3,4,6,7,1,2,4,6,4,5], 4)); | ||
|
||
/** | ||
* Required Output | ||
* [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ], [ 7, 8 ] ] | ||
* [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ], [ 7 ] ] | ||
* [ [ 1, 2, 5 ], [ 3, 4, 6 ], [ 7, 1, 2 ], [ 4, 6, 4 ], [ 5 ] ] | ||
* [ [ 1, 2, 5, 3 ], [ 4, 6, 7, 1 ], [ 2, 4, 6, 4 ], [ 5 ] ] | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* @author MadhavBahlMD | ||
* @date 16/01/2019 | ||
*/ | ||
|
||
/* ========================================= */ | ||
/* ===== Array Partition Problem In JS ===== */ | ||
/* ========================================= */ | ||
|
||
function partition (array, size) { | ||
let partitionedArray = [], i=0; | ||
|
||
while (i<array.length) { | ||
partitionedArray.push (array.slice(i, i+size)); | ||
i += size; | ||
} | ||
|
||
return partitionedArray; | ||
} | ||
|
||
// Test our partition function | ||
console.log(partition ([1,2,3,4,5,6,7,8], 2)); | ||
console.log(partition ([1,2,3,4,5,6,7], 2)); | ||
console.log(partition ([1,2,5,3,4,6,7,1,2,4,6,4,5], 3)); | ||
console.log(partition ([1,2,5,3,4,6,7,1,2,4,6,4,5], 4)); | ||
|
||
/** | ||
* Required Output | ||
* [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ], [ 7, 8 ] ] | ||
* [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ], [ 7 ] ] | ||
* [ [ 1, 2, 5 ], [ 3, 4, 6 ], [ 7, 1, 2 ], [ 4, 6, 4 ], [ 5 ] ] | ||
* [ [ 1, 2, 5, 3 ], [ 4, 6, 7, 1 ], [ 2, 4, 6, 4 ], [ 5 ] ] | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
![cover](./cover.png) | ||
|
||
# Day 20 - Array Partition | ||
|
||
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. | ||
|
||
## Example | ||
|
||
partition([1,2,3,4,5,6,7,8], 2) --> [[1, 2], [3, 4], [5, 6], [7, 8]] | ||
partition([1,2,3,4,5,6,7], 2) --> [[1, 2], [3, 4], [5, 6], [7]] | ||
|
||
## JavaScript Solution(s) | ||
|
||
### [Solution 1](./JavaScript/sol1.js) | ||
|
||
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` | ||
|
||
|
||
```js | ||
function partition (array, size) { | ||
let partitionedArray = [], | ||
toBeAdded = []; | ||
|
||
for (let i=0; i<array.length; i++) { | ||
if ((i)%size === 0) { | ||
if (toBeAdded.length > 0) partitionedArray.push (toBeAdded); | ||
toBeAdded = []; | ||
} | ||
toBeAdded.push (array[i]); | ||
} | ||
if (toBeAdded.length > 0) partitionedArray.push (toBeAdded); | ||
return partitionedArray; | ||
} | ||
``` | ||
|
||
### [Solution 2](./JavaScript/sol2.js) | ||
|
||
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. | ||
|
||
```js | ||
function partition (array, size) { | ||
let partitionedArray = []; | ||
|
||
for (let element of array) { | ||
let partition = partitionedArray[partitionedArray.length - 1]; | ||
if (!partition || partition.length === size) { | ||
partitionedArray.push ([element]); | ||
} else { | ||
lastEle.push (element); | ||
} | ||
} | ||
|
||
return partitionedArray; | ||
} | ||
``` | ||
|
||
### [Solution 3](./JavaScript/sol3.js) | ||
|
||
This method will use JavaScript's `slice` method. This is probably the easiest method due to the inbuilt function `slice`. | ||
|
||
#### How slice works | ||
|
||
array.sllice (startIndex, endIndex); will return a subarray of array including elements from startIndex to endIndex (**not including endIndex**). | ||
|
||
#### Steps | ||
|
||
- Create an empty `partitionedArray` array | ||
- run a while loop from index i=0 to less than "original" array.length | ||
- Push a `slice` of length `size` from `array` to `partitionedArray`. | ||
- `index = index+size` | ||
|
||
```js | ||
function partition (array, size) { | ||
let partitionedArray = [], i=0; | ||
while (i<array.length) { | ||
partitionedArray.push (array.slice(i, i+size)); | ||
i += size; | ||
} | ||
return partitionedArray; | ||
} | ||
``` |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.