Skip to content

Unroll matrix #1079

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
35 changes: 35 additions & 0 deletions Recursive/UnrollMatrix.js
Original file line number Diff line number Diff line change
@@ -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 => [].
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would a constant output be useful?

* @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())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please pass a visitor function instead of hardcoding console.log (or even better, turn the entire function into an iterator).

}

// 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 }
52 changes: 52 additions & 0 deletions Recursive/test/UnrollMatrix.test.js
Original file line number Diff line number Diff line change
@@ -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)
})
})