Repository navigation
Expand file tree
/
Copy patharrayPractice.js
More file actions
74 lines (60 loc) · 1.83 KB
/
Copy patharrayPractice.js
File metadata and controls
74 lines (60 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
let a = [[1,2],
[3,4]];
let b = [[1,2,3],
[4,5,6]];
let c = [[1,2],
[3,4],
[5,6]];
let d = [[1,2,3],
[ ,4,5,6],
[ ,6]];
function printDiagonals(matrix) {
let newMatrix = [];
let x = matrix[0].length;
let y = matrix.length;
for(let i = y - 1, j = 0; j < x; i--, j = i > -1 ? 0 : j + 1) {
let row = newMatrix.push([]) - 1;
for(let k = i > 0 ? i : 0, l = j; k < y && l < x; k++, l++) {
newMatrix[row].push(matrix[k][l]);
}
}
return newMatrix;
}
console.log(printDiagonals(a).map( x => x.join('')));
console.log(printDiagonals(b).map( x => x.join('')));
console.log(printDiagonals(c).map( x => x.join('')));
console.log(printDiagonals(d).map( x => x.join('')));
function mapTranspose(matrix) {
let newMatrix = [];
let maxLength = matrix.reduce( (acc, x) => acc = x.length > acc ? x.length : acc, 0);
for(let i = 0; i < maxLength; i++) {
newMatrix[i] = matrix.map( y => y[i] )
}
return newMatrix;
}
console.log(mapTranspose(a));
console.log(mapTranspose(b));
console.log(mapTranspose(c));
console.log(mapTranspose(d));
//Transpose matrix in-place
function transpose(matrix) {
let x = Math.max(...matrix.map( x => x.length));
let y = matrix.length;
let l = x > y ? x : y;
//Make matrix square
matrix.length = l;
matrix.fill((new Array(l)), y)
matrix.forEach( x => x.length = l);
//Transpose
for(let row = 1; row < matrix.length; row++)
for(let col = 0; col < row; col++)
[ matrix[row][col], matrix[col][row]] = [matrix[col][row], matrix[row][col]];
//Switch lengths
matrix.forEach( x => x.length = y);
matrix.length = x;
return matrix;
}
console.log(transpose(a));
console.log(transpose(b));
console.log(transpose(c));
console.log(transpose(d));