Find diagonal Difference of a matrix python [ hacker rank ] | extrovert.dev -->

Find diagonal Difference of a matrix python [ hacker rank ]

Find diagonal Difference of a matrix python [ hacker rank ]
Tuesday, September 21, 2021


 

Find diagonal Difference of a matrix python

  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()

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