Two-dimensional lists are a very common data structure when writing code. In lab today we simplified computing diagonals. For the post lab, you will exercise your understanding of 2-D lists by summing the values contained on the diagonal of the 2-D list.
Details
Write a function called sum_diagonal(square_list)
. The
parameter square_list
is an \(m \times m\) 2-D list of
integers, where \(m \geq 1\). Your function should return an
integer, the sum of the values along the major diagonal
of square_list
.
Make sure your program handles all necessary cases gracefully. What additional test cases should you check?
Sample Test Cases
Function Parameter | Sample Output |
---|---|
[[1, 2], [3, 4]] | 5 |
[[1, 2, 3], [4, 5, 6], [7, 8, 9]] | 15 |
[[1, 2, 3], [4, 5, 6], [7, 8, 1]] | 7 |
Submission
Submit your program as a .py file on the course Inquire page before class on Friday November 14th.