Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Open
MatthewPalmer9 wants to merge8 commits intoTheAlgorithms:master
base:master
Choose a base branch
Loading
fromMatthewPalmer9:UnrollMatrix
Open
Show file tree
Hide file tree
Changes fromall 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 deletionsDIRECTORY.md
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 deletionsRecursive/UnrollMatrix.js
View file
Open in desktop
Original file line numberDiff line numberDiff 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 hardcodingconsole.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 deletionsRecursive/test/UnrollMatrix.test.js
View file
Open in desktop
Original file line numberDiff line numberDiff 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)
})
})

[8]ページ先頭

©2009-2025 Movatter.jp