Click4Ai

22.

Easy

Write a function transpose(matrix) that transposes a 2D matrix without using .T or np.transpose(). Rows become columns and columns become rows.

Example:

Input: matrix = [[1, 2, 3],

[4, 5, 6]]

Output: [[1, 4],

[2, 5],

[3, 6]]

Constraints:

  • Matrix dimensions: 1 <= rows, cols <= 100
  • Return a list of lists
  • Test Cases

    Test Case 1
    Input: [[1, 2, 3], [4, 5, 6]]
    Expected: [[1, 4], [2, 5], [3, 6]]
    Test Case 2
    Input: [[1, 2], [3, 4]]
    Expected: [[1, 3], [2, 4]]
    Test Case 3
    Input: [[1], [2], [3]]
    Expected: [[1, 2, 3]]
    + 2 hidden test cases