Calculate the difference between two diagonals of a square matrix

”’

Created on Jun 15, 2015

@author: ishaansutaria

”’

“””

(Given N * N matrix)

1 2 3 4

1 2 3 4

1 2 3 4

1 2 3 4

1)The first diagnal is add the element whenever the row and coloum is the same

2) The second diagonal is s

“””

def findDiagonalDifference(n,list1):

    diag_sum1 = 0

    diag_sum2 = 0

    n = n – 1

    for row_index,row in enumerate(list1):

        for col_index,col in enumerate(row):

            if col_index == row_index:

                diag_sum1 += col

            

            if col_index == n:

                diag_sum2 += col

                n = n – 1

                

    return abs(diag_sum1 – diag_sum2)

    

print findDiagonalDifference(4,[[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]])

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s