-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix.py
48 lines (35 loc) · 880 Bytes
/
matrix.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'diagonalDifference' function below.
#
# The function is expected to return an INTEGER.
# The function accepts 2D_INTEGER_ARRAY arr as parameter.
#
def diagonalDifference(arr):
s1=s2=k=0
p=len(arr[0])-1
for i in range(len(arr)):
for j in range(len(arr)):
if(i==j):
s1+=arr[i][j]
while(p>-1):
s2+=arr[k][p]
p-=1
k+=1
if(s1>s2):
return s1-s2
elif(s1<s2):
return s2-s1
else:
return 0
return None
# Write your code here
if __name__ == '__main__':
#n = int(input("Enter no. of rows:").strip())
arr = [[1,2,3],[4,5,6],[7,8,9]]
result = diagonalDifference(arr)