SHARE
TWEET

HackerRank.com - diagonal-difference

furas Jul 7th, 2018 19 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. # https://www.hackerrank.com/challenges/diagonal-difference/problem
  3.  
  4. def diagonalDifference(arr):
  5.     result = 0
  6.  
  7.     n = len(arr)
  8.    
  9.     for i in range(n):
  10.         result += arr[i][i]
  11.         result -= arr[i][n-i-1]
  12.  
  13.     return abs(result)
  14.  
  15. #----------------------------------------------
  16.  
  17. tests = [ # data, expected result
  18.     ([[11, 2, 4], [4, 5, 6], [10, 8, -12]], 15),
  19.     ([[1, 2, 3], [4, 5, 6], [9, 8, 9]], 2),
  20.     ([[0]], 0),
  21. ]    
  22.  
  23. for data, expected in tests:
  24.     result = diagonalDifference(data)
  25.     print(result == expected, result, expected)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top