Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork5.7k
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:master
Are you sure you want to change the base?
Uh oh!
There was an error while loading.Please reload this page.
Unroll matrix#1079
Changes fromall commits
525af41
726ed2c
d801476
ca9cc69
79fc038
f71d5db
55ab12e
6fc47e7
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
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 => []. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Please pass a visitor function instead of hardcoding | ||
} | ||
// 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 } |
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) | ||
}) | ||
}) |