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) diff --git a/Recursive/UnrollMatrix.js b/Recursive/UnrollMatrix.js new file mode 100644 index 0000000000..3e5a6c7e8b --- /dev/null +++ b/Recursive/UnrollMatrix.js @@ -0,0 +1,35 @@ +/** + * @function UnrollMatrix + * @description Traverses/Unrolls array of arrays recursively until nothing is left. + * @param {Array} matrix - The input array of arrays + * @return {Array} matrix - The empty output array => []. + * @see https://chortle.ccsu.edu/vectorlessons/vmch13/vmch13_2.html + */ + +const UnrollMatrix = (matrix) => { + if (matrix.length === 0) return matrix + + // sweep top to right + if (matrix.length > 0) { + console.log(...matrix.shift()) + } + + // sweet top-right to bottom + if (matrix.length > 0) { + console.log(...matrix.map((arr) => arr.pop())) + } + + // sweep bottom in reverse + if (matrix.length > 0) { + console.log(...matrix.reverse().pop()) + } + + // sweep bottom-left to top + if (matrix.length > 0) { + console.log(...matrix.map((arr) => arr.reverse().shift())) + } + + 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..ef116b703e --- /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) + }) +})