Lecture
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]
Previous lessonCalculating Grades with Conditional Statements - SolutionNext lessonDiagonal Traversal of a 2D Array - Problem Solution
Lessons in this chapter · Introduction to Python Algorithms
- 1. Why You Need to Learn Algorithms?
- 2. Review of Algorithm Complexity Concepts
- 3. Life is Practical! Algorithm Problem Solving
- 4. Coding Quiz - Reverse String
- 5. String Reversal - Solution
- 6. Coding Quiz - String Case Conversion
- 7. String Case Alteration - Solution Explanation
- 8. Coding Quiz - Create a List of Powers of 2
- 9. Creating a Power of Two List - Solution Guide
- 10. Coding Quiz - Sorting Tuples by Score
- 11. Sort Tuples by Score - Solution
- 12. Coding Quiz - Find Union and Intersection
- 13. Calculating Union and Intersection - Problem Solution
- 14. Coding Quiz - Create a Dictionary from Key/Value Pairs
- 15. Create Dictionary from Key/Value Pairs - Solution
- 16. Coding Quiz - Determine Grade Using Conditional Statements
- 17. Calculating Grades with Conditional Statements - Solution
- 18. Coding Quiz - Diagonal Traverse of 2D Array
- 19. Diagonal Traversal of a 2D Array - Problem Solution
Lecture
AI Tutor
Design
Upload
Notes
Favorites
Help