|
| 1 | + |
| 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 | + |
| 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 | +``` |
0 commit comments