Skip to content

Instantly share code, notes, and snippets.

@renatojobal
Last active July 15, 2020 16:20
Show Gist options
  • Save renatojobal/dc2e92374387015a2617e3e2a3d5880a to your computer and use it in GitHub Desktop.
Save renatojobal/dc2e92374387015a2617e3e2a3d5880a to your computer and use it in GitHub Desktop.
def calculate_sum_of_corners(matrix):
"""
Función que retorna la suma de 4 esquinas de una matriz en python
"""
up_left = matrix[0][0]
up_right = matrix[0][-1]
down_left = matrix[-1][0]
down_right = matrix[-1][-1]
result = up_left + up_right + down_left + down_right
return result
def calculate_sum_of_corners2(matrix):
suma = 0
for i in range(len(matrix)):
for j in range(len(matrix[0])):
if(i == 0 and j == 0):
# Esquina superior izquierda
suma += matrix[i][j]
elif(i == 0 and j == len(matrix[0])-1):
# Esquina superior derecha
suma += matrix[i][j]
elif(i == len(matrix)-1 and j == 0):
# Esquina inferior izquierda
suma += matrix[i][j]
elif(i == len(matrix)-1 and j == len(matrix[0])-1):
# Esquina inferior derecha
suma += matrix[i][j]
return suma
#Ejemplo
target_matrix = [[100, 1, 1, 100],
[1, 1, 1, 1],
[1, 1, 1, 1],
[100, 1, 1, 100]
]
print(calculate_sum_of_corners(target_matrix))
print(calculate_sum_of_corners2(target_matrix))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment