Created
October 2, 2019 17:39
-
-
Save p8ul/bcb7305473cb10a5c0373c7706298850 to your computer and use it in GitHub Desktop.
Cartesian Product
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
https://en.wikipedia.org/wiki/Cartesian_product | |
In set theory (and, usually, in other parts of mathematics), 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. Products can | |
be specified using set-builder notation, e.g. | |
*/ | |
let A = ['x', 'y' , 'z'] | |
let B = [1, 2, 3, 4] | |
const cartesianProduct = (a, b) => { | |
const result = Array(a.length).fill(null).map(() => Array(b.length).fill(null)) | |
for (let i = 0; i < a.length; i++) { | |
for (let j = 0; j < b.length; j++) { | |
result[i][j] = a[i] + ',' + b[j] | |
} | |
} | |
return result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment