![Find diagonal Difference of a matrix python [ hacker rank ] Find diagonal Difference of a matrix python [ hacker rank ]](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjrbvMAi48IWdSEFiEnkYYXVgXFjUrsIYymiZ726v6HsDzczrs_F1rSHvkrQWM3mz3HnXq0_ZODsGpSa1Sp3afzh8XnA-hp_Hzdk7iNFlnIHh00LCimRU-q1pZ47QOovGjB_Yi_Uc0P-5w/w600/python-logo-master-v3-TM-flattened.png)
Tuesday, September 21, 2021
Sample matrix:
3
11 2 4
4 5 6
10 8 -12
Diagonal 1 : 11 , 5 ,-12
Diagonal 2: 4 ,5 ,10
Difference = |4-19|
Solution:
#!/bin/python3
import math
import os
import random
import re
import sys
def diagonalDifference(arr):
# Write your code here
p = sum(list(map(lambda x: x[arr.index(x)], arr)))
q = sum(list(map(lambda x: x[(len(arr) - 1) - arr.index(x)], arr)))
total = abs(p-q)
return total
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()
Tuesday, September 21, 2021
0 Response to Find diagonal Difference of a matrix python [ hacker rank ]
Comments are personally moderated by our team. Promotions are not encouraged.
Post a Comment