Skip to content

Commit

Permalink
Add day 19
Browse files Browse the repository at this point in the history
  • Loading branch information
MadhavBahl committed Jan 15, 2019
1 parent 9ecfd1b commit 521167a
Show file tree
Hide file tree
Showing 6 changed files with 200 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -35,6 +35,7 @@ Read [CONTRIBUTING.md](./CONTRIBUTING.md) for contribution guidelines.
16. [Day 16 -- Tower of Hanoi](./day16) -- [http://codetoexpress.tech/dc/day16/](http://codetoexpress.tech/dc/day16/)
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/)

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

Expand Down
23 changes: 23 additions & 0 deletions day19/JavaScript/cartesian_madhav.js
@@ -0,0 +1,23 @@
/**
* @author MadhavBahlMD
* @date 15/01/2018
* Method: Use nested loop to generate the 2D matrix
*/

function cartesian (arr1, arr2) {
if (arr1.length === 0 || arr2.length === 0) return null;

let cartesianProduct = [];

for (let arr1Element of arr1) {
for (let arr2Element of arr2) {
cartesianProduct.push([arr1Element, arr2Element]);
}
}

return cartesianProduct;
}

console.log (cartesian ([1, 2], [3, 4]));
console.log (cartesian ([1, 2], []));
console.log (cartesian ([1, 2, 3, 4], ['a', 'b', 'c']));
34 changes: 34 additions & 0 deletions day19/JavaScript/fisherYates.js
@@ -0,0 +1,34 @@
/**
* Fisher Yates Shuffle Algorithm
* Implemented in JavaScript by @MadhavBahlMD
* Algorithm
* Step 1: Write down the numbers from 1 through N.
* Step 2: Pick a random number k between one and the number of unstruck numbers remaining (inclusive).
* 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.
* Step 4: Repeat from step 2 until all the numbers have been struck out.
* Step 5: The sequence of numbers written down in step 3 is now a random permutation of the original numbers.
* Method used - Helper Method Recursion
*/

function fisherYates (arr) {
let shuffledArr = [];
console.log (`Original Array: ${arr}`);

function shuffle (toBeShuffled) {
// Base Case
if (toBeShuffled.length === 0) return;

// Push inro shuffled array
let index = Math.floor(Math.random()*toBeShuffled.length);
shuffledArr.push (toBeShuffled[index]);

// Shuffle the remaining array
toBeShuffled.splice (index, 1);
shuffle (toBeShuffled);
}

shuffle (arr);
console.log (`Shuffled Array: ${shuffledArr}`);
}

fisherYates ([1, 2, 3, 4, 5, 6]);
142 changes: 142 additions & 0 deletions day19/README.md
@@ -0,0 +1,142 @@
![cover](./cover.png)

# Day 19 - Array Series Part 2

**Cartesian Product and Fisher-Yates Shuffle Algorithm**

## Ques A - Cartesian Product of Two Sets

**Question** -- Given 2 sets A and B, write a program to print the cartesian product of A and B.

#### Cartesian Product

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

Source: [Wikipedia](https://en.wikipedia.org/wiki/Cartesian_product)

**Example**

```
input:
A = [1, 2]
B = [3, 4]
output:
[
[1, 3],
[1, 4],
[2, 3],
[2, 4]
]
```

## Ques B - Fisher-Yates Shuffle Algorithm

**Question** - Given an array, write a function that returns an array with shuffled elements form the original array

**Example**

```
input: [1, 2, 3, 4, 5, 6]
output: [3, 1, 4, 2, 5, 6]
```

### Fisher-Yates Shuffle Algorithm

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.

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.

#### Algorithm

1. Write down the numbers from 1 through N.
2. Pick a random number k between one and the number of unstruck numbers remaining (inclusive).
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.
4. Repeat from step 2 until all the numbers have been struck out.
5. The sequence of numbers written down in step 3 is now a random permutation of the original numbers.

source: [wikipedia (https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle)](https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle)

![ques](./ques.png)

# Solution

## Ques A

**Question** -- Given 2 sets A and B, write a program to print the cartesian product of A and B.

### JavaScript Implementation

#### [Solution by @MadhavBahlMD](./JavaScript/cartesian_madhav.js)

```js
/**
* @author MadhavBahlMD
* @date 15/01/2018
* Method: Use nested loop to generate the 2D matrix
*/

function cartesian (arr1, arr2) {
if (arr1.length === 0 || arr2.length === 0) return null;

let cartesianProduct = [];

for (let arr1Element of arr1) {
for (let arr2Element of arr2) {
cartesianProduct.push([arr1Element, arr2Element]);
}
}

return cartesianProduct;
}

console.log (cartesian ([1, 2], [3, 4]));
console.log (cartesian ([1, 2], []));
console.log (cartesian ([1, 2, 3, 4], ['a', 'b', 'c']));
```

***

## Ques B

**Question** -- Given an array, write a function that returns an array with shuffled elements form the original array

### JavaScript Implementation

#### [Solution](./JavaScript/fisherYates_madhav.js)

```js
/**
* Fisher Yates Shuffle Algorithm
* Implemented in JavaScript by @MadhavBahlMD
* Algorithm
* Step 1: Write down the numbers from 1 through N.
* Step 2: Pick a random number k between one and the number of unstruck numbers remaining (inclusive).
* 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.
* Step 4: Repeat from step 2 until all the numbers have been struck out.
* Step 5: The sequence of numbers written down in step 3 is now a random permutation of the original numbers.
* Method used - Helper Method Recursion
*/

function fisherYates (arr) {
let shuffledArr = [];
console.log (`Original Array: ${arr}`);

function shuffle (toBeShuffled) {
// Base Case
if (toBeShuffled.length === 0) return;

// Push inro shuffled array
let index = Math.floor(Math.random()*toBeShuffled.length);
shuffledArr.push (toBeShuffled[index]);

// Shuffle the remaining array
toBeShuffled.splice (index, 1);
shuffle (toBeShuffled);
}

shuffle (arr);
console.log (`Shuffled Array: ${shuffledArr}`);
}

fisherYates ([1, 2, 3, 4, 5, 6]);
```
Binary file added day19/cover.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added day19/ques.png
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 521167a

Please sign in to comment.