Hackerrank Linear Algebra Solution
1 min read

Hackerrank Linear Algebra Solution

Hackerrank Linear Algebra Solution

The NumPy module also comes with a number of built-in routines for linear algebra calculations. These can be found in the sub-module linalg.


The linalg.det tool computes the determinant of an array.

print numpy.linalg.det([[1 , 2], [2, 1]])       #Output : -3.0


The linalg.eig computes the eigenvalues and right eigenvectors of a square array.

vals, vecs = numpy.linalg.eig([[1 , 2], [2, 1]])
print vals                                      #Output : [ 3. -1.]
print vecs                                      #Output : [[ 0.70710678 -0.70710678]
                                                #          [ 0.70710678  0.70710678]]


The linalg.inv tool computes the (multiplicative) inverse of a matrix.

print numpy.linalg.inv([[1 , 2], [2, 1]])       #Output : [[-0.33333333  0.66666667]
                                                #          [ 0.66666667 -0.33333333]]

Other routines can be found here


You are given a square matrix  with dimensions X. Your task is to find the determinant. Note: Round the answer to 2 places after the decimal.

Input Format

The first line contains the integer .
The next  lines contains the  space separated elements of array .

Output Format

Print the determinant of .

Sample Input

1.1 1.1
1.1 1.1

Sample Output


Solution in python3

Approach 1.

import numpy
print(round(numpy.linalg.det(numpy.array([input().split() for _ in range(int(input()))],float)),2))

Approach 2.

import numpy
array=[list(map(float,input().split())) for i in range(int(input()))]

Approach 3.

import numpy
N = int(input())
A = numpy.array([input().split() for _ in range(N)], float)

Solution in python

Approach 1.

import numpy
print round(numpy.linalg.det([map(float,raw_input().split()) for _ in range(input())]),2)

Approach 2.

import numpy
print round(numpy.linalg.det([ map(float, raw_input().split()) for i in range(int(raw_input()))]),2)

Approach 3.

import numpy
for i in range(n):
print round(numpy.linalg.det(numpy.array(m)),2)

Enjoying these posts? Subscribe for more

Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

That's okay. But without advertising-income, we can't keep making this site awesome.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add thepoorcoder.com to your ad blocking whitelist or disable your adblocking software.