“Python Diagonal Sum” Code-Antworten

Python Diagonal Sum

# A simple Python3 program to find
# sum of diagonals
MAX = 100
 
def printDiagonalSums(mat, n):
 
    principal = 0
    secondary = 0
    for i in range(0, n):
        principal += mat[i][i]
        secondary += mat[i][n - i - 1]
         
    print("Principal Diagonal:", principal)
    print("Secondary Diagonal:", secondary)
 
# Driver code
a = [[ 1, 2, 3, 4 ],
     [ 5, 6, 7, 8 ],
     [ 1, 2, 3, 4 ],
     [ 5, 6, 7, 8 ]]
printDiagonalSums(a, 4)
 
# This code is contributed
# by ihritik
Busy Bird

Summe der diagonalen Numpy


arr= np.arange(16)
arr= np.reshape(arr,(4,4))

a=np.diagonal(arr, 0,0,1)

result=sum(a)
print(result)
Ferry_Morris

Ähnliche Antworten wie “Python Diagonal Sum”

Fragen ähnlich wie “Python Diagonal Sum”

Weitere verwandte Antworten zu “Python Diagonal Sum” auf Python

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen