Click4Ai

27.

Easy

Write a function get_diagonal(matrix) that extracts the main diagonal elements from a square matrix without using np.diag().

Example:

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

[4, 5, 6],

[7, 8, 9]]

Output: [1, 5, 9]

Constraints:

  • Matrix is always square (n×n)
  • 1 <= n <= 100
  • Return a list
  • Test Cases

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