Skip to content

Instantly share code, notes, and snippets.

@dlucidone
Last active February 12, 2019 03:27
Show Gist options
  • Save dlucidone/c9d9fc24ecdd256b5c7809920f04e118 to your computer and use it in GitHub Desktop.
Save dlucidone/c9d9fc24ecdd256b5c7809920f04e118 to your computer and use it in GitHub Desktop.
Array Spiral
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
var matrix = [[1,2,3], [4,5,6], [7,8,9]];
var row = currentRow = matrix.length, column = currentColumn = matrix[0].length;
var matrixClone = [];
while(currentRow > row/2 ){
// traverse row forward
for(var i = (column - currentColumn); i < currentColumn ; i++) {
matrixClone.push(matrix[row - currentRow][i])
}
// traverse column downward
for(var i = (row - currentRow + 1); i < currentRow ; i++) {
matrixClone.push(matrix[i][currentColumn - 1]);
}
// traverse row backward
for(var i = currentColumn - 1; i > (column - currentColumn) ; i--) {
matrixClone.push(matrix[currentRow - 1][i - 1])
}
// traverse column upward
for(var i = currentRow - 1; i > (row - currentRow + 1) ; i--) {
matrixClone.push(matrix[i - 1][column - currentColumn])
}
currentRow--;
currentColumn--;
}
console.log(matrixClone)
var input = [[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]];
var input1 = [[1, 2 ],
[5, 6 ],
[9, 10],
[13, 14]];
var result =[];
function run(input) {
if (input.length == 0 ) {
return result;
}
var a = [[], []];
while
console.log(a.toString())
// add the first row to result
result = result.concat(input.shift());
// add the last element of each remaining row
input.forEach(function(rightEnd) {
result.push(rightEnd.pop());
});
// add the last row in reverse order
result = result.concat(input.pop().reverse());
// add the first element in each remaining row (going upwards)
var tmp = [];
input.forEach(function(leftEnd) {
tmp.push(leftEnd.shift());
});
result = result.concat(tmp.reverse());
return run(input);
}
var result = run(input1);
console.log('result', result);
</script>
<script id="jsbin-source-javascript" type="text/javascript">var matrix = [[1,2,3], [4,5,6], [7,8,9]];
var row = currentRow = matrix.length, column = currentColumn = matrix[0].length;
var matrixClone = [];
while(currentRow > row/2 ){
// traverse row forward
for(var i = (column - currentColumn); i < currentColumn ; i++) {
matrixClone.push(matrix[row - currentRow][i])
}
// traverse column downward
for(var i = (row - currentRow + 1); i < currentRow ; i++) {
matrixClone.push(matrix[i][currentColumn - 1]);
}
// traverse row backward
for(var i = currentColumn - 1; i > (column - currentColumn) ; i--) {
matrixClone.push(matrix[currentRow - 1][i - 1])
}
// traverse column upward
for(var i = currentRow - 1; i > (row - currentRow + 1) ; i--) {
matrixClone.push(matrix[i - 1][column - currentColumn])
}
currentRow--;
currentColumn--;
}
console.log(matrixClone)
var input = [[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]];
var input1 = [[1, 2 ],
[5, 6 ],
[9, 10],
[13, 14]];
var result =[];
function run(input) {
if (input.length == 0 ) {
return result;
}
var a = [[], []];
while
console.log(a.toString())
// add the first row to result
result = result.concat(input.shift());
// add the last element of each remaining row
input.forEach(function(rightEnd) {
result.push(rightEnd.pop());
});
// add the last row in reverse order
result = result.concat(input.pop().reverse());
// add the first element in each remaining row (going upwards)
var tmp = [];
input.forEach(function(leftEnd) {
tmp.push(leftEnd.shift());
});
result = result.concat(tmp.reverse());
return run(input);
}
var result = run(input1);
console.log('result', result);
</script></body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment