Skip to content

Commit 88e5a92

Browse files
committed
Add day 20
1 parent ef1a7b4 commit 88e5a92

File tree

7 files changed

+190
-0
lines changed

7 files changed

+190
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Read [CONTRIBUTING.md](./CONTRIBUTING.md) for contribution guidelines.
3636
17. [Day 17 -- N Queens Problem](./day17) -- [http://codetoexpress.tech/dc/day17/](http://codetoexpress.tech/dc/day17/)
3737
18. [Day 18 -- Frequency Count and Check Power N](./day18) -- [http://codetoexpress.tech/dc/day18/](http://codetoexpress.tech/dc/day18/)
3838
19. [Day 19 -- Cartesian Product and Shuffle Algorithm](./day19) -- [http://codetoexpress.tech/dc/day19/](http://codetoexpress.tech/dc/day19/)
39+
20. [Day 20 -- Array Partition](./day20) -- [http://codetoexpress.tech/dc/day20/](http://codetoexpress.tech/dc/day20/)
3940

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

day20/JavaScript/sol1.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* @author MadhavBahlMD
3+
* @date 16/01/2019
4+
*/
5+
6+
/* ========================================= */
7+
/* ===== Array Partition Problem In JS ===== */
8+
/* ========================================= */
9+
10+
function partition (array, size) {
11+
let partitionedArray = [],
12+
toBeAdded = [];
13+
14+
for (let i=0; i<array.length; i++) {
15+
if ((i)%size === 0) {
16+
if (toBeAdded.length > 0) partitionedArray.push (toBeAdded);
17+
toBeAdded = [];
18+
}
19+
toBeAdded.push (array[i]);
20+
}
21+
if (toBeAdded.length > 0) partitionedArray.push (toBeAdded);
22+
return partitionedArray;
23+
}
24+
25+
// Test our partition function
26+
console.log(partition ([1,2,3,4,5,6,7,8], 2));
27+
console.log(partition ([1,2,3,4,5,6,7], 2));
28+
console.log(partition ([1,2,5,3,4,6,7,1,2,4,6,4,5], 3));
29+
console.log(partition ([1,2,5,3,4,6,7,1,2,4,6,4,5], 4));
30+
31+
/**
32+
* Required Output
33+
* [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ], [ 7, 8 ] ]
34+
* [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ], [ 7 ] ]
35+
* [ [ 1, 2, 5 ], [ 3, 4, 6 ], [ 7, 1, 2 ], [ 4, 6, 4 ], [ 5 ] ]
36+
* [ [ 1, 2, 5, 3 ], [ 4, 6, 7, 1 ], [ 2, 4, 6, 4 ], [ 5 ] ]
37+
*/

day20/JavaScript/sol2.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* @author MadhavBahlMD
3+
* @date 16/01/2019
4+
*/
5+
6+
/* ========================================= */
7+
/* ===== Array Partition Problem In JS ===== */
8+
/* ========================================= */
9+
10+
function partition (array, size) {
11+
let partitionedArray = [];
12+
13+
for (let element of array) {
14+
let partition = partitionedArray[partitionedArray.length - 1];
15+
16+
if (!partition || partition.length === size) {
17+
partitionedArray.push ([element]);
18+
} else {
19+
lastEle.push (element);
20+
}
21+
}
22+
23+
return partitionedArray;
24+
}
25+
26+
// Test our partition function
27+
console.log(partition ([1,2,3,4,5,6,7,8], 2));
28+
console.log(partition ([1,2,3,4,5,6,7], 2));
29+
console.log(partition ([1,2,5,3,4,6,7,1,2,4,6,4,5], 3));
30+
console.log(partition ([1,2,5,3,4,6,7,1,2,4,6,4,5], 4));
31+
32+
/**
33+
* Required Output
34+
* [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ], [ 7, 8 ] ]
35+
* [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ], [ 7 ] ]
36+
* [ [ 1, 2, 5 ], [ 3, 4, 6 ], [ 7, 1, 2 ], [ 4, 6, 4 ], [ 5 ] ]
37+
* [ [ 1, 2, 5, 3 ], [ 4, 6, 7, 1 ], [ 2, 4, 6, 4 ], [ 5 ] ]
38+
*/

day20/JavaScript/sol3.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @author MadhavBahlMD
3+
* @date 16/01/2019
4+
*/
5+
6+
/* ========================================= */
7+
/* ===== Array Partition Problem In JS ===== */
8+
/* ========================================= */
9+
10+
function partition (array, size) {
11+
let partitionedArray = [], i=0;
12+
13+
while (i<array.length) {
14+
partitionedArray.push (array.slice(i, i+size));
15+
i += size;
16+
}
17+
18+
return partitionedArray;
19+
}
20+
21+
// Test our partition function
22+
console.log(partition ([1,2,3,4,5,6,7,8], 2));
23+
console.log(partition ([1,2,3,4,5,6,7], 2));
24+
console.log(partition ([1,2,5,3,4,6,7,1,2,4,6,4,5], 3));
25+
console.log(partition ([1,2,5,3,4,6,7,1,2,4,6,4,5], 4));
26+
27+
/**
28+
* Required Output
29+
* [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ], [ 7, 8 ] ]
30+
* [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ], [ 7 ] ]
31+
* [ [ 1, 2, 5 ], [ 3, 4, 6 ], [ 7, 1, 2 ], [ 4, 6, 4 ], [ 5 ] ]
32+
* [ [ 1, 2, 5, 3 ], [ 4, 6, 7, 1 ], [ 2, 4, 6, 4 ], [ 5 ] ]
33+
*/

day20/README.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
![cover](./cover.png)
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+
```

day20/cover.png

135 KB
Loading

day20/ques.png

891 KB
Loading

0 commit comments

Comments
 (0)