Skip to content

Instantly share code, notes, and snippets.

@NEbere
Created September 18, 2017 16:31
Show Gist options
  • Save NEbere/c9bf163e236f9e551a4bef83b86e43b1 to your computer and use it in GitHub Desktop.
Save NEbere/c9bf163e236f9e551a4bef83b86e43b1 to your computer and use it in GitHub Desktop.
# Python3 solution to https://www.hackerrank.com/challenges/diagonal-difference/problem
# test data
first_array = [11, 2, 4]
second_array = [4, 5, 6]
third_array = [10, 8, -12]
sumArray = []
sumArray.append(first_array)
sumArray.append(second_array)
sumArray.append(third_array)
def findDiagonalDifference(array):
primaryDiagonal = []
secondaryDiagonal = []
index = 0
last_index = len(array[0]) - 1
for arrayRow in array:
primaryDiagonal.append(arrayRow[index])
secondaryDiagonal.append(arrayRow[last_index])
last_index -= 1
index += 1
sum_primary = sum(primaryDiagonal)
sum_secondary = sum(secondaryDiagonal)
abs_difference = abs(sum_primary - sum_secondary)
findDiagonalDifference(sumArray)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment