Reputation: 83
I am working on a HackerRank problem and I don't understand some of the logic:
if (i == j):
left += a[i][j]
The above is saying if the row/column indices are the same([1,1], [2,2], [3,3]), append value found at those coordinates to the list 'left'
I don't understand the logic in the code below. To me it looks like it's saying append values found at the coordinates where row index + column index = 3 (n-1) but I don't think that's right. What would the code below translate to?
if (i + j) == (n - 1):
right += a[i][j]
Below is the function with sample inputs a and n.
a = [[ 1, 2, 3, 4 ],
[ 5, 6, 7, 8 ],
[ 1, 2, 3, 4 ],
[ 5, 6, 7, 9 ]]
n = 4
def xsum(a, n):
left = 0
right = 0
for i in range(0, n):
for j in range(0, n):
if (i == j):
left += a[i][j]
if (i + j) == (n - 1):
right += a[i][j]
return (abs(left-right))
Upvotes: 1
Views: 3417
Reputation: 1
def diagonalDifference(arr):
n = len(arr)
left_to_right = 0
right_to_left = 0
for i in range(n):
left_to_right += arr[i][i]
right_to_left += arr[i][n - (i + 1)]
return abs(left_to_right - right_to_left)
Upvotes: 0
Reputation: 11
def diagonalDifference(arr):
mat = len(arr[0]) #type of matrix e.g(3by3)
left_sum = 0
right_sum =0
k =mat-1 #rightmost element accessor
for i in range(mat):
left_sum += arr[i][i]
for j in range(mat):
right_sum += arr[j][k]
k-=1
return abs(right_sum - left_sum)
Upvotes: 1
Reputation: 36440
This looks an awful lot like it's working too hard.
If we want to calculate that "left" diagonal.
a = [[ 1, 2, 3, 4 ],
[ 5, 6, 7, 8 ],
[ 1, 2, 3, 4 ],
[ 5, 6, 7, 9 ]]
left = []
for i in range(0, 4):
left.append(a[i][i])
Or:
left = [a[i][i] for i in range(0, 4)]
To find the "right" diagonal.
right = [a[i][3 - i] for i in range(0, 4)]
If we need to see how this generates a set of indices to index into a
, we can with the following:
>>> [(i, 3 - i) for i in range(0, 4)]
[(0, 3), (1, 2), (2, 1), (3, 0)]
Which yields: [4, 7, 2, 5]
. If you need it reversed.
right = [a[3 - i][i] for i in range(0, 4)]
This yields: [5, 2, 7, 4]
.
If we need the sum of the left diagonal, we just need to use the sum
function: left_sum = sum(left)
I've written these examples specifically for a 4x4 matrix. It should be straightforward to generalize this for larger or smaller matrices.
Upvotes: 3
Reputation: 19223
What would the code below translate to?
The code leverages the fact that an element is on the right diagonal if and only if the row and column indices sum to n - 1
. It then adds the value at the given (i, j)
pair to right
.
This can be visualized using the following matrix: each entry is the sum of its row and column index:
print([[i + j for i in range(4)] for j in range(4)])
#[[0, 1, 2, 3],
# [1, 2, 3, 4],
# [2, 3, 4, 5],
# [3, 4, 5, 6]]
# (notice that the n - 1 == 3, and the 3s are only on the antidiagonal!)
This is a question about correctness, which is distinct from efficiency. If you're wondering whether this code is efficient, the answer is no: see Chris's answer.
Upvotes: 2