-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path05-Diagonal_Difference.py
48 lines (34 loc) · 1.08 KB
/
05-Diagonal_Difference.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
# -*- coding: utf-8 -*-
'''
Created on Mon Feb 21 2022
@author: Carlos Páez
'''
#
# Complete the `diagonalDifference` function below
#
# The function is expected to return a INTEGER
# The function accepts 2D_INTEGER_ARRAY ar as parameter
#
def diagonalDifference(arr):
"""
Given a square matrix, calculate the absolute difference between the sums of its diagonals
:param arr: an array of integers
:return: The absolute value of the difference between the sum of the diagonals.
"""
left_to_right = 0
right_to_left = 0
x, y = 0, len(arr) - 1
for i in range(len(arr)):
left_to_right += arr[i][x] if -100 <= arr[i][x] <= 100 else 0
right_to_left += arr[i][y] if -100 <= arr[i][y] <= 100 else 0
x += 1
y -= 1
return abs(left_to_right - right_to_left)
if __name__ == '__main__':
fptr = open('./output.txt', 'w')
n = int(input().strip())
arr = []
for _ in range(n):
arr.append(list(map(int, input().rstrip().split())))
result = diagonalDifference(arr)
fptr.write(str(result) + '\n')