transposeMatrix.js with lots of dry run (**** IMPORTANT****)

 /*Transpose Matrix

You are given m lists. Each list contains n elements. Represented as a matrix, this has m
rows and n columns. Your task is to transpose the matrix and output the result.

Write a function with name transposeMatrix which takes a matrix as list of lists as input
returns a transposed matrix as list of lists.

Matrix transpose
Given a matrix:

a b c d

e f g h
the transpose is:

a e
b f
c g
d h
Input
The first line contains m, denoting the number of lists

This is followed by m lines each containing n integers separated by space

Output
n lines should contain each row of the matrix, with the elements separated by a space

Example
Input:

3
1 2 3 4
5 6 7 8
9 10 11 12

Output:

1 5 9
2 6 10
3 7 11
4 8 12

You just have. to return transformed matrix as a list, printing is taken care of by the judge.
*/

let fs = require("fs");

let data = fs.readFileSync(0, 'utf-8');
let idx = 0;
data = data.split('\n');

function readLine() {
idx++;
return data[idx - 1].trim();
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

let noOfRows = parseInt(readLine()); // 3

let matrix = []; // we will push the row arrays here afterwards

// This part is only to take the input

for (let i = 0; i < noOfRows; i++) // 1. i=0<3 true, 2. i=1<3 true , 3. i=2<3 true , 4. i=3<3 false exit the forloop
{
let rowArr = readLine().split(" ");

matrix.push(rowArr);

// // console.log(matrix); when i=0 [ [ '1', '2', '3', '4' ] ] green color elements

// when i = 1 [['1', '2', '3', '4'], ['5', '6', '7', '8']]

// when i = 2 [
// [ '1', '2', '3', '4' ],
// [ '5', '6', '7', '8' ],
// [ '9', '10', '11', '12' ]
// ]


}

// console.log(matrix); [
// [ '1', '2', '3', '4' ], green color elements
// [ '5', '6', '7', '8' ],
// [ '9', '10', '11', '12' ]
// ]

This part is the actual Operation:

let noOfColoumns = matrix[0].length; // matrix['1', '2', '3', '4'].length i.e 4

let transposedMatrix = [];// we will push the transposed Matrix here after the operations

for (let j = 0; j < noOfColoumns; j++) // 1. j=0<4 true
{

let temp = [];

for (let i = 0; i < noOfRows; i++) // 1.i=0<3 true
{

temp.push(matrix[i][j]);

/* console.log(temp); [ '1' ] when j=0, i=0 green color elements
[ '1', '5' ] when j=0, i=1 j and i should interchange positions
[ '1', '5', '9' ]when j=0, i=2 please understand while revising ok

[ '2' ] when j=1, i=0
[ '2', '6' ] when j=1, i=1
[ '2', '6', '10' ]when j=1, i=2

[ '3' ] when j=2, i=0
[ '3', '7' ] when j=2, i=1
[ '3', '7', '11' ]when j=2, i=2

[ '4' ] when j=3, i=0
[ '4', '8' ] when j=3, i=1
[ '4', '8', '12' ]when j=3, i=2
*/
}

// console.log(temp); [ '1', '5', '9' ]
// [ '2', '6', '10' ]
// [ '3', '7', '11' ]
// [ '4', '8', '12' ]


transposedMatrix.push(temp);

}

// console.log(transposedMatrix); [
// [ '1', '5', '9' ], green color elements
// [ '2', '6', '10' ],
// [ '3', '7', '11' ],
// [ '4', '8', '12' ]
// ]

for (let i = 0; i < transposedMatrix.length; i++) // 1.i=0<4 true, 2.i=1<4 true, 3. i=2<4 true, 4. i=3<4 true
{
console.log(transposedMatrix[i].join(" ")) // 1. transposedMatrix[0].join(" ")
// 2. transposedMatrix[1].join(" ")
// 3. transposedMatrix[2].join(" ")
// 4. transposedMatrix[3].join(" ")
// .join(" ") will convert the array into a string
and replace the comma with space

}

///////////////////////////////////////////////////////////////////////////////////////////////
TERMINAL:








Comments