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

Cerinta

Find determinant of the following matrix -

<img src="https://s3.amazonaws.com/hr-challenge-images/0/1445329844-96c8d95796-Screenshotfrom2015-10-20140034.png" title="Screenshot from 2015-10-20 14:00:34.png" />

Your answer’s determinant value should look like this:

<pre>
5
</pre>

Cod sursa

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