Coding Quiz - Diagonal Traverse of 2D Array
In this coding quiz, you need to write a function that retrieves the diagonal elements of a 2D array
and returns a list composed of these elements.
The given 2D array will be a square array
, and you need to create a list with the elements located along the diagonal from the top-left corner to the bottom-right corner of the array.
Write Code
def solution(matrix): # Write your code here return
Constraints
-
The given 2D array is always square (NxN).
-
Each element in the array is an integer.
-
The size of the array ranges from at least 1x1 to a maximum of 100x100.
Example Input and Output
-
Input:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
-
Output:
[1, 5, 9]
-
Input:
[[1, 2], [3, 4]]
-
Output:
[1, 4]
Lecture
AI Tutor
Publish
Design
Upload
Notes
Favorites
Help