-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiagonal_difference.py
More file actions
65 lines (45 loc) · 1.53 KB
/
Copy pathdiagonal_difference.py
File metadata and controls
65 lines (45 loc) · 1.53 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/bin/python3
"""
Solution to this problem:
https://www.hackerrank.com/challenges/diagonal-difference/problem
"""
import math
import os
import random
import re
import sys
import unittest
def diagonalDifference(arr):
sum_main_diagonal = 0
sum_secondary_diagonal = 0
for row in range(len(arr)):
column = len(arr) - row - 1
sum_main_diagonal += arr[row][row]
sum_secondary_diagonal += arr[row][column]
return abs(sum_main_diagonal - sum_secondary_diagonal)
class TestDiagonalDifference(unittest.TestCase):
def test_diagonal_difference_zero(self):
arr = [[1, 1, 1], [1, 1, 1], [1, 1, 1]]
result = diagonalDifference(arr)
expected = 0
self.assertEqual(result, expected)
def test_diagonal_difference_larger_main_diagonal(self):
arr = [[2, 1, 1], [1, 1, 1], [1, 1, 2]]
result = diagonalDifference(arr)
expected = 2
self.assertEqual(result, expected)
def test_diagonal_difference_larger_secondary(self):
arr = [[1, 1, 2], [1, 1, 1], [2, 1, 1]]
result = diagonalDifference(arr)
expected = 2
self.assertEqual(result, expected)
#unittest.main(verbosity=2)
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], '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')
fptr.close()