-
-
Notifications
You must be signed in to change notification settings - Fork 5.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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MatthewPalmer9
wants to merge
8
commits into
TheAlgorithms:master
Choose a base branch
from
MatthewPalmer9:UnrollMatrix
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Unroll matrix #1079
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
525af41
Added UnrollMatrix algorithm with tests to 'Recursive' folder
MatthewPalmer9 726ed2c
new branch
MatthewPalmer9 d801476
Updated Documentation in README.md
ca9cc69
Add UnrollMatrix with tests.
MatthewPalmer9 79fc038
Merge branch 'UnrollMatrix' of https://github.com/MatthewPalmer9/Java…
MatthewPalmer9 f71d5db
Add UnrollMatrix with tests.
MatthewPalmer9 55ab12e
Correction to @function name from Matrix to UnrollMatrix
MatthewPalmer9 6fc47e7
Correction to @return value in comments.
MatthewPalmer9 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 => []. | ||
* @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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?