Question: use python and one loop to solve this problem Q6: Secondary Trace You are given a function that takes a square array, matrix, and returns
Q6: Secondary Trace You are given a function that takes a square array, matrix, and returns the sum of the secondary diagonal values for matrix. This function employs a nested 2D for-loop. Write a function secondary trace single loop that solves the same problem using only one for-loop. For example, on the picture below secondary diagonal consists of values: a03, a12, a21, a30. a00 a01 a02 a03 a10 all a12 a13 a20 a21 a22 a23 a30 a31 a32 a33 def secondary trace (matrix) : 1 Return sum of values on secondary diagonal. >>mO[, 0], [0, 111 > ml[, 1, 1], [2, 2, 21,[3, 3, 311 >>> m2 [[3, 4],[6, 911 >>> secondary trace (m0) >>> secondary trace (ml) >>> secondary trace (m2) 10 Tr TT 11 for i in range (len (matrix)): for j in range (1en (matrix)): if ilen (matrix) - -1: sum += matrix [i] [j] return sum def seconday trace single loop (matrix): IT TT 11 Return sum of values on secondary diagonal >>> mo = [[1, 0], [0, 1]] >>> m2 = [[3, 4], [6, 911 >>> seconday trace single loop (mO) >>>seconday trace single loop (mi) >>> seconday trace single loop (m2) 10 1 YI # YOUR CODE IS HERE
Step by Step Solution
There are 3 Steps involved in it
Sure To solve the problem using a single loop we can iterate over the indices of the matrix and ... View full answer
Get step-by-step solutions from verified subject matter experts
