From 525af414251c351c69a9fd466774fc07907fa778 Mon Sep 17 00:00:00 2001 From: Matthew Palmer Date: Sat, 6 Aug 2022 17:36:46 -0400 Subject: [PATCH 1/7] Added UnrollMatrix algorithm with tests to 'Recursive' folder --- Recursive/UnrollMatrix.js | 23 +++++++++++++ Recursive/test/UnrollMatrix.test.js | 52 +++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 Recursive/UnrollMatrix.js create mode 100644 Recursive/test/UnrollMatrix.test.js diff --git a/Recursive/UnrollMatrix.js b/Recursive/UnrollMatrix.js new file mode 100644 index 0000000000..6fcc4acdf1 --- /dev/null +++ b/Recursive/UnrollMatrix.js @@ -0,0 +1,23 @@ +/** + * @function Matrix + * @description Traverses/Unrolls array of arrays recursively until nothing is left. + * @param {Array} matrix - The input array of arrays + * @return {void}. + */ + +const UnrollMatrix = (matrix) => { + if (matrix.length === 0) return matrix + + // sweep top to right + matrix.length > 0 ? console.log(...matrix.shift()) : null; + // sweet top-right to bottom + matrix.length > 0 ? console.log(...matrix.map((arr) => arr.pop())) : null; + // sweep bottom in reverse + matrix.length > 0 ? console.log(...matrix.reverse().pop()) : null; + // sweep bottom-left to top + matrix.length > 0 ? console.log(...matrix.map((arr) => arr.reverse().shift())) : null; + + return UnrollMatrix(matrix) +} + +export { UnrollMatrix } diff --git a/Recursive/test/UnrollMatrix.test.js b/Recursive/test/UnrollMatrix.test.js new file mode 100644 index 0000000000..565eecb4ca --- /dev/null +++ b/Recursive/test/UnrollMatrix.test.js @@ -0,0 +1,52 @@ +import { UnrollMatrix } from "../UnrollMatrix"; + +describe('UnrollMatrix', () => { + const emptyMatrix = [ + [] + ]; + + const evenMatrix = [ + [1, 2, 3, 4], + [12,13,14,5], + [11,16,15,6], + [10,9, 8, 7] + ]; + + const unevenMatrix = [ + [1, 2, 3, 4], + [13,14,15,16, 5], + [12,18,17, 6], + [11,10, 9, 8, 7] + ]; + + const singleArrayMatrix = [ + [1, 2, 3, 4] + ]; + + const deeplyNestedMatrix = [ + [[[],[],[],[[],[]]]], + [[[[],[],[]],[[],[]]]], + [[],[],[[],[]],[]], + [[],[],[],[],[[[],[],[]]]] + ]; + + it('should return matrix length of 0 when given an empty matrix', () => { + expect(UnrollMatrix(emptyMatrix).length).toBe(0); + }) + + it('should return matrix length of 0 when given an even matrix', () => { + expect(UnrollMatrix(evenMatrix).length).toBe(0); + }); + + it('should return matrix length of 0 when given an uneven matrix', () => { + expect(UnrollMatrix(unevenMatrix).length).toBe(0); + }); + + it('should return matrix length of 0 when given a matrix with one array', () => { + expect(UnrollMatrix(singleArrayMatrix).length).toBe(0); + }); + + it('should return matrix length of 0 when given a deeply nested matrix', () => { + expect(UnrollMatrix(deeplyNestedMatrix).length).toBe(0); + }); +}); \ No newline at end of file From 726ed2c5e154423ca04eb906f24a3f13d8fc9939 Mon Sep 17 00:00:00 2001 From: Matthew Palmer Date: Sat, 6 Aug 2022 17:39:08 -0400 Subject: [PATCH 2/7] new branch --- Recursive/UnrollMatrix.js | 19 +++++++-- Recursive/test/UnrollMatrix.test.js | 64 ++++++++++++++--------------- 2 files changed, 47 insertions(+), 36 deletions(-) diff --git a/Recursive/UnrollMatrix.js b/Recursive/UnrollMatrix.js index 6fcc4acdf1..7647cb1b40 100644 --- a/Recursive/UnrollMatrix.js +++ b/Recursive/UnrollMatrix.js @@ -9,13 +9,24 @@ const UnrollMatrix = (matrix) => { if (matrix.length === 0) return matrix // sweep top to right - matrix.length > 0 ? console.log(...matrix.shift()) : null; + if (matrix.length > 0) { + console.log(...matrix.shift()) + } + // sweet top-right to bottom - matrix.length > 0 ? console.log(...matrix.map((arr) => arr.pop())) : null; + if (matrix.length > 0) { + console.log(...matrix.map((arr) => arr.pop())) + } + // sweep bottom in reverse - matrix.length > 0 ? console.log(...matrix.reverse().pop()) : null; + if (matrix.length > 0) { + console.log(...matrix.reverse().pop()) + } + // sweep bottom-left to top - matrix.length > 0 ? console.log(...matrix.map((arr) => arr.reverse().shift())) : null; + if (matrix.length > 0) { + console.log(...matrix.map((arr) => arr.reverse().shift())) + } return UnrollMatrix(matrix) } diff --git a/Recursive/test/UnrollMatrix.test.js b/Recursive/test/UnrollMatrix.test.js index 565eecb4ca..ef116b703e 100644 --- a/Recursive/test/UnrollMatrix.test.js +++ b/Recursive/test/UnrollMatrix.test.js @@ -1,52 +1,52 @@ -import { UnrollMatrix } from "../UnrollMatrix"; +import { UnrollMatrix } from '../UnrollMatrix' describe('UnrollMatrix', () => { const emptyMatrix = [ [] - ]; - + ] + const evenMatrix = [ - [1, 2, 3, 4], - [12,13,14,5], - [11,16,15,6], - [10,9, 8, 7] - ]; - + [1, 2, 3, 4], + [12, 13, 14, 5], + [11, 16, 15, 6], + [10, 9, 8, 7] + ] + const unevenMatrix = [ - [1, 2, 3, 4], - [13,14,15,16, 5], - [12,18,17, 6], - [11,10, 9, 8, 7] - ]; + [1, 2, 3, 4], + [13, 14, 15, 16, 5], + [12, 18, 17, 6], + [11, 10, 9, 8, 7] + ] const singleArrayMatrix = [ [1, 2, 3, 4] - ]; + ] const deeplyNestedMatrix = [ - [[[],[],[],[[],[]]]], - [[[[],[],[]],[[],[]]]], - [[],[],[[],[]],[]], - [[],[],[],[],[[[],[],[]]]] - ]; + [[[], [], [], [[], []]]], + [[[[], [], []], [[], []]]], + [[], [], [[], []], []], + [[], [], [], [], [[[], [], []]]] + ] it('should return matrix length of 0 when given an empty matrix', () => { - expect(UnrollMatrix(emptyMatrix).length).toBe(0); + expect(UnrollMatrix(emptyMatrix).length).toBe(0) }) - + it('should return matrix length of 0 when given an even matrix', () => { - expect(UnrollMatrix(evenMatrix).length).toBe(0); - }); - + expect(UnrollMatrix(evenMatrix).length).toBe(0) + }) + it('should return matrix length of 0 when given an uneven matrix', () => { - expect(UnrollMatrix(unevenMatrix).length).toBe(0); - }); + expect(UnrollMatrix(unevenMatrix).length).toBe(0) + }) it('should return matrix length of 0 when given a matrix with one array', () => { - expect(UnrollMatrix(singleArrayMatrix).length).toBe(0); - }); + expect(UnrollMatrix(singleArrayMatrix).length).toBe(0) + }) it('should return matrix length of 0 when given a deeply nested matrix', () => { - expect(UnrollMatrix(deeplyNestedMatrix).length).toBe(0); - }); -}); \ No newline at end of file + expect(UnrollMatrix(deeplyNestedMatrix).length).toBe(0) + }) +}) From d8014767f4c78267ae376458b99be003eb6fcdec Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sat, 6 Aug 2022 21:40:32 +0000 Subject: [PATCH 3/7] Updated Documentation in README.md --- DIRECTORY.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 5a491bf570..5137de06ff 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -170,6 +170,7 @@ * [IsEven](Maths/IsEven.js) * [IsOdd](Maths/IsOdd.js) * [IsPronic](Maths/IsPronic.js) + * [JugglerSequence](Maths/JugglerSequence.js) * [LeapYear](Maths/LeapYear.js) * [LinearSieve](Maths/LinearSieve.js) * [LucasSeries](Maths/LucasSeries.js) @@ -236,6 +237,7 @@ * [Palindrome](Recursive/Palindrome.js) * [SubsequenceRecursive](Recursive/SubsequenceRecursive.js) * [TowerOfHanoi](Recursive/TowerOfHanoi.js) + * [UnrollMatrix](Recursive/UnrollMatrix.js) * **Search** * [BinarySearch](Search/BinarySearch.js) * [ExponentialSearch](Search/ExponentialSearch.js) From ca9cc69778a706257b1eadea28815a1285f5340b Mon Sep 17 00:00:00 2001 From: Matthew Palmer Date: Sat, 6 Aug 2022 17:41:34 -0400 Subject: [PATCH 4/7] Add UnrollMatrix with tests. --- Recursive/UnrollMatrix.js | 19 +++++++-- Recursive/test/UnrollMatrix.test.js | 64 ++++++++++++++--------------- 2 files changed, 47 insertions(+), 36 deletions(-) diff --git a/Recursive/UnrollMatrix.js b/Recursive/UnrollMatrix.js index 6fcc4acdf1..7647cb1b40 100644 --- a/Recursive/UnrollMatrix.js +++ b/Recursive/UnrollMatrix.js @@ -9,13 +9,24 @@ const UnrollMatrix = (matrix) => { if (matrix.length === 0) return matrix // sweep top to right - matrix.length > 0 ? console.log(...matrix.shift()) : null; + if (matrix.length > 0) { + console.log(...matrix.shift()) + } + // sweet top-right to bottom - matrix.length > 0 ? console.log(...matrix.map((arr) => arr.pop())) : null; + if (matrix.length > 0) { + console.log(...matrix.map((arr) => arr.pop())) + } + // sweep bottom in reverse - matrix.length > 0 ? console.log(...matrix.reverse().pop()) : null; + if (matrix.length > 0) { + console.log(...matrix.reverse().pop()) + } + // sweep bottom-left to top - matrix.length > 0 ? console.log(...matrix.map((arr) => arr.reverse().shift())) : null; + if (matrix.length > 0) { + console.log(...matrix.map((arr) => arr.reverse().shift())) + } return UnrollMatrix(matrix) } diff --git a/Recursive/test/UnrollMatrix.test.js b/Recursive/test/UnrollMatrix.test.js index 565eecb4ca..ef116b703e 100644 --- a/Recursive/test/UnrollMatrix.test.js +++ b/Recursive/test/UnrollMatrix.test.js @@ -1,52 +1,52 @@ -import { UnrollMatrix } from "../UnrollMatrix"; +import { UnrollMatrix } from '../UnrollMatrix' describe('UnrollMatrix', () => { const emptyMatrix = [ [] - ]; - + ] + const evenMatrix = [ - [1, 2, 3, 4], - [12,13,14,5], - [11,16,15,6], - [10,9, 8, 7] - ]; - + [1, 2, 3, 4], + [12, 13, 14, 5], + [11, 16, 15, 6], + [10, 9, 8, 7] + ] + const unevenMatrix = [ - [1, 2, 3, 4], - [13,14,15,16, 5], - [12,18,17, 6], - [11,10, 9, 8, 7] - ]; + [1, 2, 3, 4], + [13, 14, 15, 16, 5], + [12, 18, 17, 6], + [11, 10, 9, 8, 7] + ] const singleArrayMatrix = [ [1, 2, 3, 4] - ]; + ] const deeplyNestedMatrix = [ - [[[],[],[],[[],[]]]], - [[[[],[],[]],[[],[]]]], - [[],[],[[],[]],[]], - [[],[],[],[],[[[],[],[]]]] - ]; + [[[], [], [], [[], []]]], + [[[[], [], []], [[], []]]], + [[], [], [[], []], []], + [[], [], [], [], [[[], [], []]]] + ] it('should return matrix length of 0 when given an empty matrix', () => { - expect(UnrollMatrix(emptyMatrix).length).toBe(0); + expect(UnrollMatrix(emptyMatrix).length).toBe(0) }) - + it('should return matrix length of 0 when given an even matrix', () => { - expect(UnrollMatrix(evenMatrix).length).toBe(0); - }); - + expect(UnrollMatrix(evenMatrix).length).toBe(0) + }) + it('should return matrix length of 0 when given an uneven matrix', () => { - expect(UnrollMatrix(unevenMatrix).length).toBe(0); - }); + expect(UnrollMatrix(unevenMatrix).length).toBe(0) + }) it('should return matrix length of 0 when given a matrix with one array', () => { - expect(UnrollMatrix(singleArrayMatrix).length).toBe(0); - }); + expect(UnrollMatrix(singleArrayMatrix).length).toBe(0) + }) it('should return matrix length of 0 when given a deeply nested matrix', () => { - expect(UnrollMatrix(deeplyNestedMatrix).length).toBe(0); - }); -}); \ No newline at end of file + expect(UnrollMatrix(deeplyNestedMatrix).length).toBe(0) + }) +}) From f71d5db7a0c6031a63460e5dbb5df6364f45a9e6 Mon Sep 17 00:00:00 2001 From: Matthew Palmer Date: Sat, 6 Aug 2022 17:47:28 -0400 Subject: [PATCH 5/7] Add UnrollMatrix with tests. --- Recursive/UnrollMatrix.js | 1 + 1 file changed, 1 insertion(+) diff --git a/Recursive/UnrollMatrix.js b/Recursive/UnrollMatrix.js index 7647cb1b40..7fce3aa2ff 100644 --- a/Recursive/UnrollMatrix.js +++ b/Recursive/UnrollMatrix.js @@ -3,6 +3,7 @@ * @description Traverses/Unrolls array of arrays recursively until nothing is left. * @param {Array} matrix - The input array of arrays * @return {void}. + * @see https://chortle.ccsu.edu/vectorlessons/vmch13/vmch13_2.html */ const UnrollMatrix = (matrix) => { From 55ab12e4519857aad71937c0d1ac608f17effcd7 Mon Sep 17 00:00:00 2001 From: Matthew Palmer Date: Sat, 6 Aug 2022 20:12:51 -0400 Subject: [PATCH 6/7] Correction to @function name from Matrix to UnrollMatrix --- Recursive/UnrollMatrix.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Recursive/UnrollMatrix.js b/Recursive/UnrollMatrix.js index 7fce3aa2ff..58653e96cc 100644 --- a/Recursive/UnrollMatrix.js +++ b/Recursive/UnrollMatrix.js @@ -1,5 +1,5 @@ /** - * @function Matrix + * @function UnrollMatrix * @description Traverses/Unrolls array of arrays recursively until nothing is left. * @param {Array} matrix - The input array of arrays * @return {void}. From 6fc47e7830a29fcc7552a904d9a9da7a3618670b Mon Sep 17 00:00:00 2001 From: Matthew Palmer Date: Sat, 6 Aug 2022 20:17:30 -0400 Subject: [PATCH 7/7] Correction to @return value in comments. --- Recursive/UnrollMatrix.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Recursive/UnrollMatrix.js b/Recursive/UnrollMatrix.js index 58653e96cc..3e5a6c7e8b 100644 --- a/Recursive/UnrollMatrix.js +++ b/Recursive/UnrollMatrix.js @@ -2,7 +2,7 @@ * @function UnrollMatrix * @description Traverses/Unrolls array of arrays recursively until nothing is left. * @param {Array} matrix - The input array of arrays - * @return {void}. + * @return {Array} matrix - The empty output array => []. * @see https://chortle.ccsu.edu/vectorlessons/vmch13/vmch13_2.html */