@galencorey/

Flipping a matrix 90 in place

JavaScript

No description

fork
loading

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.32032266094141026","path":"main.js","file":{"path":"main.js","content":{"asEncoding":{"base64":"ZnVuY3Rpb24gZmxpcChtYXRyaXgpewogIHZhciBOID0gbWF0cml4Lmxlbmd0aDsKICB2YXIgdGVtcDsgCiAgCiAgZm9yICh2YXIgTGF5ZXIgPSAwOyBMYXllciA8IChOLzIpOyBMYXllciArKyl7CiAgICBmb3IgKHZhciBQb3MgPSBMYXllcjsgUG9zIDwgTiAtIExheWVyIC0gMTsgUG9zKyspewogICAgICB0ZW1wID0gbWF0cml4W0xheWVyXVtQb3NdOyAKICAgICAgbWF0cml4W0xheWVyXVtQb3NdID0gbWF0cml4W04tUG9zLTFdW0xheWVyXTsgCiAgICAgIG1hdHJpeFtOLVBvcy0xXVtMYXllcl0gPSBtYXRyaXhbTi1MYXllci0xXVtOLVBvcy0xXTsgIAogICAgICBtYXRyaXhbTi1MYXllci0xXVtOLVBvcy0xXSA9IG1hdHJpeFtQb3NdW04tTGF5ZXItMV07IAogICAgICBtYXRyaXhbUG9zXVtOLUxheWVyLTFdID0gdGVtcDsgCiAgICB9CiAgfQogIAogIHJldHVybiBtYXRyaXg7IAp9Cgpjb25zb2xlLmxvZyhmbGlwKFtbMSwyLDMsNF0sWzUsNiw3LDhdLFs5LDEwLDExLDEyXSxbMTMsMTQsMTUsMTZdXSkpOwpjb25zb2xlLmxvZygnXG4nKTsKY29uc29sZS5sb2coZmxpcChbWzEsMiwzXSxbNCw1LDZdLFs3LDgsOV1dKSk7IAo="},"asBuffer":null},"loaded":true}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
function flip(matrix){
  var N = matrix.length;
  var temp; 
  
  for (var Layer = 0; Layer < (N/2); Layer ++){
    for (var Pos = Layer; Pos < N - Layer - 1; Pos++){
      temp = matrix[Layer][Pos]; 
      matrix[Layer][Pos] = matrix[N-Pos-1][Layer]; 
      matrix[N-Pos-1][Layer] = matrix[N-Layer-1][N-Pos-1];  
      matrix[N-Layer-1][N-Pos-1] = matrix[Pos][N-Layer-1]; 
      matrix[Pos][N-Layer-1] = temp; 
    }
  }
  
  return matrix; 
}

console.log(flip([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]));
console.log('\n');
console.log(flip([[1,2,3],[4,5,6],[7,8,9]])); 
Native Browser JavaScript