Skip to content

Commit 521167a

Browse files
committed
Add day 19
1 parent 9ecfd1b commit 521167a

File tree

6 files changed

+200
-0
lines changed

6 files changed

+200
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Read [CONTRIBUTING.md](./CONTRIBUTING.md) for contribution guidelines.
3535
16. [Day 16 -- Tower of Hanoi](./day16) -- [http://codetoexpress.tech/dc/day16/](http://codetoexpress.tech/dc/day16/)
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/)
38+
19. [Day 19 -- Cartesian Product and Shuffle Algorithm](./day19) -- [http://codetoexpress.tech/dc/day19/](http://codetoexpress.tech/dc/day19/)
3839

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

day19/JavaScript/cartesian_madhav.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @author MadhavBahlMD
3+
* @date 15/01/2018
4+
* Method: Use nested loop to generate the 2D matrix
5+
*/
6+
7+
function cartesian (arr1, arr2) {
8+
if (arr1.length === 0 || arr2.length === 0) return null;
9+
10+
let cartesianProduct = [];
11+
12+
for (let arr1Element of arr1) {
13+
for (let arr2Element of arr2) {
14+
cartesianProduct.push([arr1Element, arr2Element]);
15+
}
16+
}
17+
18+
return cartesianProduct;
19+
}
20+
21+
console.log (cartesian ([1, 2], [3, 4]));
22+
console.log (cartesian ([1, 2], []));
23+
console.log (cartesian ([1, 2, 3, 4], ['a', 'b', 'c']));

day19/JavaScript/fisherYates.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Fisher Yates Shuffle Algorithm
3+
* Implemented in JavaScript by @MadhavBahlMD
4+
* Algorithm
5+
* Step 1: Write down the numbers from 1 through N.
6+
* Step 2: Pick a random number k between one and the number of unstruck numbers remaining (inclusive).
7+
* Step 3: Counting from the low end, strike out the kth number not yet struck out, and write it down at the end of a separate list.
8+
* Step 4: Repeat from step 2 until all the numbers have been struck out.
9+
* Step 5: The sequence of numbers written down in step 3 is now a random permutation of the original numbers.
10+
* Method used - Helper Method Recursion
11+
*/
12+
13+
function fisherYates (arr) {
14+
let shuffledArr = [];
15+
console.log (`Original Array: ${arr}`);
16+
17+
function shuffle (toBeShuffled) {
18+
// Base Case
19+
if (toBeShuffled.length === 0) return;
20+
21+
// Push inro shuffled array
22+
let index = Math.floor(Math.random()*toBeShuffled.length);
23+
shuffledArr.push (toBeShuffled[index]);
24+
25+
// Shuffle the remaining array
26+
toBeShuffled.splice (index, 1);
27+
shuffle (toBeShuffled);
28+
}
29+
30+
shuffle (arr);
31+
console.log (`Shuffled Array: ${shuffledArr}`);
32+
}
33+
34+
fisherYates ([1, 2, 3, 4, 5, 6]);

day19/README.md

+142
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
![cover](./cover.png)
2+
3+
# Day 19 - Array Series Part 2
4+
5+
**Cartesian Product and Fisher-Yates Shuffle Algorithm**
6+
7+
## Ques A - Cartesian Product of Two Sets
8+
9+
**Question** -- Given 2 sets A and B, write a program to print the cartesian product of A and B.
10+
11+
#### Cartesian Product
12+
13+
In set theory a Cartesian product is a mathematical operation that returns a set (or product set or simply product) from multiple sets. That is, for sets A and B, the Cartesian product A × B is the set of all ordered pairs (a, b) where a ∈ A and b ∈ B
14+
15+
Source: [Wikipedia](https://en.wikipedia.org/wiki/Cartesian_product)
16+
17+
**Example**
18+
19+
```
20+
input:
21+
A = [1, 2]
22+
B = [3, 4]
23+
output:
24+
[
25+
[1, 3],
26+
[1, 4],
27+
[2, 3],
28+
[2, 4]
29+
]
30+
```
31+
32+
## Ques B - Fisher-Yates Shuffle Algorithm
33+
34+
**Question** - Given an array, write a function that returns an array with shuffled elements form the original array
35+
36+
**Example**
37+
38+
```
39+
input: [1, 2, 3, 4, 5, 6]
40+
output: [3, 1, 4, 2, 5, 6]
41+
```
42+
43+
### Fisher-Yates Shuffle Algorithm
44+
45+
The Fisher–Yates shuffle is an algorithm for generating a random permutation of a finite sequence—in plain terms, the algorithm shuffles the sequence. The algorithm effectively puts all the elements into a hat; it continually determines the next element by randomly drawing an element from the hat until no elements remain. The algorithm produces an unbiased permutation: every permutation is equally likely.
46+
47+
The Fisher–Yates shuffle is named after Ronald Fisher and Frank Yates, who first described it, and is also known as the Knuth shuffle after Donald Knuth.
48+
49+
#### Algorithm
50+
51+
1. Write down the numbers from 1 through N.
52+
2. Pick a random number k between one and the number of unstruck numbers remaining (inclusive).
53+
3. Counting from the low end, strike out the kth number not yet struck out, and write it down at the end of a separate list.
54+
4. Repeat from step 2 until all the numbers have been struck out.
55+
5. The sequence of numbers written down in step 3 is now a random permutation of the original numbers.
56+
57+
source: [wikipedia (https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle)](https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle)
58+
59+
![ques](./ques.png)
60+
61+
# Solution
62+
63+
## Ques A
64+
65+
**Question** -- Given 2 sets A and B, write a program to print the cartesian product of A and B.
66+
67+
### JavaScript Implementation
68+
69+
#### [Solution by @MadhavBahlMD](./JavaScript/cartesian_madhav.js)
70+
71+
```js
72+
/**
73+
* @author MadhavBahlMD
74+
* @date 15/01/2018
75+
* Method: Use nested loop to generate the 2D matrix
76+
*/
77+
78+
function cartesian (arr1, arr2) {
79+
if (arr1.length === 0 || arr2.length === 0) return null;
80+
81+
let cartesianProduct = [];
82+
83+
for (let arr1Element of arr1) {
84+
for (let arr2Element of arr2) {
85+
cartesianProduct.push([arr1Element, arr2Element]);
86+
}
87+
}
88+
89+
return cartesianProduct;
90+
}
91+
92+
console.log (cartesian ([1, 2], [3, 4]));
93+
console.log (cartesian ([1, 2], []));
94+
console.log (cartesian ([1, 2, 3, 4], ['a', 'b', 'c']));
95+
```
96+
97+
***
98+
99+
## Ques B
100+
101+
**Question** -- Given an array, write a function that returns an array with shuffled elements form the original array
102+
103+
### JavaScript Implementation
104+
105+
#### [Solution](./JavaScript/fisherYates_madhav.js)
106+
107+
```js
108+
/**
109+
* Fisher Yates Shuffle Algorithm
110+
* Implemented in JavaScript by @MadhavBahlMD
111+
* Algorithm
112+
* Step 1: Write down the numbers from 1 through N.
113+
* Step 2: Pick a random number k between one and the number of unstruck numbers remaining (inclusive).
114+
* Step 3: Counting from the low end, strike out the kth number not yet struck out, and write it down at the end of a separate list.
115+
* Step 4: Repeat from step 2 until all the numbers have been struck out.
116+
* Step 5: The sequence of numbers written down in step 3 is now a random permutation of the original numbers.
117+
* Method used - Helper Method Recursion
118+
*/
119+
120+
function fisherYates (arr) {
121+
let shuffledArr = [];
122+
console.log (`Original Array: ${arr}`);
123+
124+
function shuffle (toBeShuffled) {
125+
// Base Case
126+
if (toBeShuffled.length === 0) return;
127+
128+
// Push inro shuffled array
129+
let index = Math.floor(Math.random()*toBeShuffled.length);
130+
shuffledArr.push (toBeShuffled[index]);
131+
132+
// Shuffle the remaining array
133+
toBeShuffled.splice (index, 1);
134+
shuffle (toBeShuffled);
135+
}
136+
137+
shuffle (arr);
138+
console.log (`Shuffled Array: ${shuffledArr}`);
139+
}
140+
141+
fisherYates ([1, 2, 3, 4, 5, 6]);
142+
```

day19/cover.png

145 KB
Loading

day19/ques.png

263 KB
Loading

0 commit comments

Comments
 (0)