From 6fff99d44eb870b06c0d8e5797a28cea70779c99 Mon Sep 17 00:00:00 2001 From: xinzhe822 Date: Sun, 7 Aug 2022 22:07:21 +0800 Subject: [PATCH 1/6] Added cartesianProduct.js --- Maths/CartesianProduct.js | 24 ++++++++++++++++++++++++ Maths/test/CartesianProduct.test.js | 19 +++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 Maths/CartesianProduct.js create mode 100644 Maths/test/CartesianProduct.test.js diff --git a/Maths/CartesianProduct.js b/Maths/CartesianProduct.js new file mode 100644 index 0000000000..285578649f --- /dev/null +++ b/Maths/CartesianProduct.js @@ -0,0 +1,24 @@ +/** + * @function cartesianProduct + * @description Generate Cartesian Product of Two Sets. + * @param {*[]} setA -First set + * @param {*[]} setB -Second set + * @return {*[]} -Cartesian Product of setA and setB + */ +const cartesianProduct = (setA, setB) => { + // Check if input sets are not empty. + if (!setA || !setB || !setA.length || !setB.length) { + return null; + } + const product = []; + + for (let indexA = 0; indexA < setA.length; indexA += 1) { + for (let indexB = 0; indexB < setB.length; indexB += 1) { + product.push([setA[indexA], setB[indexB]]); + } + } + + return product +} + +export { cartesianProduct } \ No newline at end of file diff --git a/Maths/test/CartesianProduct.test.js b/Maths/test/CartesianProduct.test.js new file mode 100644 index 0000000000..f773dc6f6e --- /dev/null +++ b/Maths/test/CartesianProduct.test.js @@ -0,0 +1,19 @@ +import {cartesianProduct} from '../cartesianProduct'; + +describe('cartesianProduct', () => { + it('should return null if not enough info for calculation', () => { + const product1 = cartesianProduct([1], null) + const product2 = cartesianProduct([], null) + + expect(product1).toBeNull() + expect(product2).toBeNull() + }) + + it('should calculate the product of two sets', () => { + const product1 = cartesianProduct([1], [1]) + const product2 = cartesianProduct([1, 2], [3, 5]) + + expect(product1).toEqual([[1, 1]]); + expect(product2).toEqual([[1, 3], [1, 5], [2, 3], [2, 5]]) + }) +}) \ No newline at end of file From ee7ef6769c79928b672d094f9b1c11d0b3bdecd0 Mon Sep 17 00:00:00 2001 From: xinzhe822 Date: Sun, 7 Aug 2022 22:18:27 +0800 Subject: [PATCH 2/6] Added CartesianProduct.test.js --- Maths/test/CartesianProduct.test.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Maths/test/CartesianProduct.test.js b/Maths/test/CartesianProduct.test.js index f773dc6f6e..3d5c35a138 100644 --- a/Maths/test/CartesianProduct.test.js +++ b/Maths/test/CartesianProduct.test.js @@ -11,9 +11,11 @@ describe('cartesianProduct', () => { it('should calculate the product of two sets', () => { const product1 = cartesianProduct([1], [1]) - const product2 = cartesianProduct([1, 2], [3, 5]) + const product2 = cartesianProduct([1,2], [3]) + const product3 = cartesianProduct([1, 2], [3, 4]) - expect(product1).toEqual([[1, 1]]); - expect(product2).toEqual([[1, 3], [1, 5], [2, 3], [2, 5]]) + expect(product1).toEqual([[1, 1]]) + expect(product2).toEqual([[1, 3],[2, 3]]) + expect(product3).toEqual([[1, 3], [1, 4], [2, 3], [2, 4]]) }) }) \ No newline at end of file From 374df971418306fd00594c626b00f11ac34597e6 Mon Sep 17 00:00:00 2001 From: xinzhe822 Date: Mon, 8 Aug 2022 01:01:55 +0800 Subject: [PATCH 3/6] Solve import error --- Maths/test/CartesianProduct.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Maths/test/CartesianProduct.test.js b/Maths/test/CartesianProduct.test.js index 3d5c35a138..ec0f0ff07e 100644 --- a/Maths/test/CartesianProduct.test.js +++ b/Maths/test/CartesianProduct.test.js @@ -1,4 +1,4 @@ -import {cartesianProduct} from '../cartesianProduct'; +import {cartesianProduct} from '../CartesianProduct'; describe('cartesianProduct', () => { it('should return null if not enough info for calculation', () => { From 2e9740e274f9dcf843eaf7dd38bc6030be1ff6aa Mon Sep 17 00:00:00 2001 From: xinzhe822 Date: Mon, 8 Aug 2022 01:17:02 +0800 Subject: [PATCH 4/6] Format code style --- Maths/CartesianProduct.js | 8 ++++---- Maths/test/CartesianProduct.test.js | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Maths/CartesianProduct.js b/Maths/CartesianProduct.js index 285578649f..42acc8f9d8 100644 --- a/Maths/CartesianProduct.js +++ b/Maths/CartesianProduct.js @@ -8,17 +8,17 @@ const cartesianProduct = (setA, setB) => { // Check if input sets are not empty. if (!setA || !setB || !setA.length || !setB.length) { - return null; + return null } - const product = []; + const product = [] for (let indexA = 0; indexA < setA.length; indexA += 1) { for (let indexB = 0; indexB < setB.length; indexB += 1) { - product.push([setA[indexA], setB[indexB]]); + product.push([setA[indexA], setB[indexB]]) } } return product } -export { cartesianProduct } \ No newline at end of file +export { cartesianProduct } diff --git a/Maths/test/CartesianProduct.test.js b/Maths/test/CartesianProduct.test.js index ec0f0ff07e..e5db00a6b6 100644 --- a/Maths/test/CartesianProduct.test.js +++ b/Maths/test/CartesianProduct.test.js @@ -1,4 +1,4 @@ -import {cartesianProduct} from '../CartesianProduct'; +import { cartesianProduct } from '../CartesianProduct' describe('cartesianProduct', () => { it('should return null if not enough info for calculation', () => { @@ -11,11 +11,11 @@ describe('cartesianProduct', () => { it('should calculate the product of two sets', () => { const product1 = cartesianProduct([1], [1]) - const product2 = cartesianProduct([1,2], [3]) + const product2 = cartesianProduct([1, 2], [3]) const product3 = cartesianProduct([1, 2], [3, 4]) expect(product1).toEqual([[1, 1]]) - expect(product2).toEqual([[1, 3],[2, 3]]) + expect(product2).toEqual([[1, 3], [2, 3]]) expect(product3).toEqual([[1, 3], [1, 4], [2, 3], [2, 4]]) }) -}) \ No newline at end of file +}) From 8796336512a4e3c614a3e2fc0bd01e8b9680baa6 Mon Sep 17 00:00:00 2001 From: vinayak Date: Thu, 25 Aug 2022 17:37:17 +0530 Subject: [PATCH 5/6] Update CartesianProduct.js --- Maths/CartesianProduct.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Maths/CartesianProduct.js b/Maths/CartesianProduct.js index 42acc8f9d8..4eaff00772 100644 --- a/Maths/CartesianProduct.js +++ b/Maths/CartesianProduct.js @@ -8,13 +8,13 @@ const cartesianProduct = (setA, setB) => { // Check if input sets are not empty. if (!setA || !setB || !setA.length || !setB.length) { - return null + return {} } const product = [] - - for (let indexA = 0; indexA < setA.length; indexA += 1) { - for (let indexB = 0; indexB < setB.length; indexB += 1) { - product.push([setA[indexA], setB[indexB]]) + + for(let elementA of setA){ + for(let elementB of setB){ + product.push([ elementA, elementB]) } } From 6cd36d1644aa9d4dfe33b2d7cd7ab6733e445fb5 Mon Sep 17 00:00:00 2001 From: vinayak Date: Sun, 28 Aug 2022 03:03:56 +0530 Subject: [PATCH 6/6] Update CartesianProduct.js --- Maths/CartesianProduct.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Maths/CartesianProduct.js b/Maths/CartesianProduct.js index 4eaff00772..65eb2f0ce8 100644 --- a/Maths/CartesianProduct.js +++ b/Maths/CartesianProduct.js @@ -8,7 +8,7 @@ const cartesianProduct = (setA, setB) => { // Check if input sets are not empty. if (!setA || !setB || !setA.length || !setB.length) { - return {} + return [] } const product = [] @@ -17,7 +17,6 @@ const cartesianProduct = (setA, setB) => { product.push([ elementA, elementB]) } } - return product }