Created
September 18, 2017 16:31
-
-
Save NEbere/c9bf163e236f9e551a4bef83b86e43b1 to your computer and use it in GitHub Desktop.
Python3 solution to https://www.hackerrank.com/challenges/diagonal-difference/problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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