Skip to content

Commit

Permalink
Add day 20
Browse files Browse the repository at this point in the history
  • Loading branch information
MadhavBahl committed Jan 16, 2019
1 parent ef1a7b4 commit 88e5a92
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Read [CONTRIBUTING.md](./CONTRIBUTING.md) for contribution guidelines.
17. [Day 17 -- N Queens Problem](./day17) -- [http://codetoexpress.tech/dc/day17/](http://codetoexpress.tech/dc/day17/)
18. [Day 18 -- Frequency Count and Check Power N](./day18) -- [http://codetoexpress.tech/dc/day18/](http://codetoexpress.tech/dc/day18/)
19. [Day 19 -- Cartesian Product and Shuffle Algorithm](./day19) -- [http://codetoexpress.tech/dc/day19/](http://codetoexpress.tech/dc/day19/)
20. [Day 20 -- Array Partition](./day20) -- [http://codetoexpress.tech/dc/day20/](http://codetoexpress.tech/dc/day20/)

## [More Problems](./BONUS/README.md)

Expand Down
37 changes: 37 additions & 0 deletions day20/JavaScript/sol1.js
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 ] ]
*/
38 changes: 38 additions & 0 deletions day20/JavaScript/sol2.js
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 ] ]
*/
33 changes: 33 additions & 0 deletions day20/JavaScript/sol3.js
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 ] ]
*/
81 changes: 81 additions & 0 deletions day20/README.md
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;
}
```
Binary file added day20/cover.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added day20/ques.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 88e5a92

Please sign in to comment.