Soluție HackerRank pentru Determinant of the matrix #1, subdomeniul Linear Algebra Foundations, în Python 3. Include cerința formatată, exemple…

  • Problemă: Determinant of the matrix #1
  • Domeniu: Linear Algebra Foundations
  • Limbaj: Python 3

Challenge: Determinant of the matrix #1

Subdomeniu: Linear Algebra Foundations (linear-algebra-foundations)

Scor cont: 5.0 / 5

Submission status: Accepted

Submission score: 1.0

Submission ID: 464717159

Limbaj: python3

Link challenge: https://www.hackerrank.com/challenges/determinant-of-the-matrix-1/problem

Cerință

Find determinant of the following matrix -

Screenshot from 2015-10-20 14:00:34.png

Your answer’s determinant value should look like this:

<pre>
5
</pre>

Cod sursă

A = [
  [3,0,0,-2,4],
  [0,2,0,0,0],
  [0,-1,0,5,-3],
  [-4,0,1,0,6],
  [0,-1,0,3,2]
]
def det(A):
  N = len(A)
  if N == 1: return A[0][0]
  return sum(
    (-1)**i * v * det([[v for j,v in enumerate(ls) if j != i] for ls in A[1:]])
    for i,v in enumerate(A[0])
    if v
  )

print(det(A))
HackerRank Linear Algebra Foundations – Determinant of the matrix #1