Created
October 3, 2022 02:51
-
-
Save kulicuu/4d7f72589344d54a8299b73d6eb2c140 to your computer and use it in GitHub Desktop.
Quick sketch code spiral output matrix...
This file contains hidden or 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
const c = console.log.bind(console) | |
let arr_0 = [ | |
[1, 2, 3, 10], | |
[4, 5, 6, 11], | |
[7, 8, 9, 11], | |
[12, 13, 14, 15] | |
] | |
let arr_1 = [ | |
[18, 19, 20, 21] | |
] | |
let arr_2 = [ | |
[30], | |
[21], | |
[45], | |
[34] | |
] | |
let arr_3 = [ | |
[84] | |
] | |
let arr_4 = [ | |
[1, 2, 3], | |
[4, 5, 6], | |
[7, 8, 9] | |
] | |
let arr_5 = [ | |
[1, 2, 3, 10], | |
[4, 5, 6, 11], | |
[7, 8, 9, 11], | |
[12, 13, 14, 15] | |
] | |
let arr_6 = [ | |
[1, 2, 3], | |
[4, 5, 6], | |
[7, 8, 9] | |
] | |
spiral_read_outside_mat(arr_0) | |
mat_strip_border(arr_0) | |
c('\n') | |
spiral_read_outside_mat(arr_0) | |
c('\n') | |
spiral_read_outside_mat(arr_1) | |
c('\n') | |
spiral_read_outside_mat(arr_2) | |
c('\n') | |
spiral_read_outside_mat(arr_3) | |
c('\n') | |
spiral_read_outside_mat(arr_4) | |
c('\n \n \n Real Test:') | |
spiral_read_mat(arr_5) | |
c('\n \n \n Real Test 2:') | |
spiral_read_mat(arr_6) | |
function spiral_read_mat(arr) { | |
// edge cases: | |
// the matrix cull might leave a single row or column vector, in that case we wouldn't want to run all | |
// of these things | |
// we definitely do the top row to the right. if this is the only element in the mat we done. | |
// if col_len > 1 we can do the down column part | |
// the route back only happens if row_len > 1 | |
// and the route back up will happen also if row_len > 1 | |
while (arr.length > 0 && arr[0].length > 0) { | |
spiral_read_outside_mat(arr) | |
mat_strip_border(arr) | |
} | |
} | |
function mat_strip_border(arr) { | |
arr.splice(0, 1) | |
arr.splice(arr.length - 1, 1) | |
arr.map((row) => { | |
row.splice(0, 1) | |
row.splice(row.length - 1, 1) | |
}) | |
} | |
function spiral_read_outside_mat(arr) { | |
let row_len = arr[0].length | |
let column_len = arr.length | |
for (let i = 0; i < row_len; i++) { | |
c(arr[0][i]) | |
} | |
for (let j = 1; j < column_len; j++) { | |
c(arr[j][row_len - 1]) | |
} | |
if (row_len > 1 && column_len > 1) { | |
for (let i = (row_len - 2); i > -1; i--) { | |
c(arr[column_len - 1][i]) | |
} | |
for (let j = column_len - 2; j > 0; j--) { | |
c(arr[j][0]) | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment