print numpy.linalg.det([[1 , 2], [2, 1]]) #Output : -3.0
HackerRank Python Solution - Numpy Topic - Linear Algebra
HackerRank Python Solution - Numpy Topic - Polynomials
print numpy.poly([-1, 1, 1, 10]) #Output : [ 1 -11 9 11 -10]
HackerRank Python Solution - Numpy Topic - Inner and Outer
import numpy
A = numpy.array([0, 1])
B = numpy.array([3, 4])
print numpy.inner(A, B) #Output : 4
HackerRank Python Solution - Numpy Topic - Dot and Cross
import numpy
A = numpy.array([ 1, 2 ])
B = numpy.array([ 3, 4 ])
print numpy.dot(A, B) #Output : 11
HackerRank Python Solution - Numpy Topic - Mean, Var, and Std
import numpy
my_array = numpy.array([ [1, 2], [3, 4] ])
print numpy.mean(my_array, axis = 0) #Output : [ 2. 3.]
print numpy.mean(my_array, axis = 1) #Output : [ 1.5 3.5]
print numpy.mean(my_array, axis = None) #Output : 2.5
print numpy.mean(my_array) #Output : 2.5
HackerRank Python Solution - Numpy Topic - Min and Max
import numpy
my_array = numpy.array([[2, 5],
[3, 7],
[1, 3],
[4, 0]])
print numpy.min(my_array, axis = 0) #Output : [1 0]
print numpy.min(my_array, axis = 1) #Output : [2 3 1 0]
print numpy.min(my_array, axis = None) #Output : 0
print numpy.min(my_array) #Output : 0
HackerRank Python Solution - Numpy Topic - Sum and Prod
import numpy
my_array = numpy.array([ [1, 2], [3, 4] ])
print numpy.sum(my_array, axis = 0) #Output : [4 6]
print numpy.sum(my_array, axis = 1) #Output : [3 7]
print numpy.sum(my_array, axis = None) #Output : 10
print numpy.sum(my_array) #Output : 10
HackerRank Python Solution - Numpy Topic - Floor, Ceil and Rint
import numpy
my_array = numpy.array([1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9])
print numpy.floor(my_array) #[ 1. 2. 3. 4. 5. 6. 7. 8. 9.]
HackerRank Python Solution - Numpy Topic - Shape and Reshape
import numpy
my__1D_array = numpy.array([1, 2, 3, 4, 5])
print my_1D_array.shape #(5,) -> 1 row and 5 columns
my__2D_array = numpy.array([[1, 2],[3, 4],[6,5]])
print my_2D_array.shape #(3, 2) -> 3 rows and 2 columns
HackerRank Python Solution - Numpy Topic - Array Mathematics
import numpy
a = numpy.array([1,2,3,4], float)
b = numpy.array([5,6,7,8], float)
print a + b #[ 6. 8. 10. 12.]
print numpy.add(a, b) #[ 6. 8. 10. 12.]
print a - b #[-4. -4. -4. -4.]
print numpy.subtract(a, b) #[-4. -4. -4. -4.]
print a * b #[ 5. 12. 21. 32.]
print numpy.multiply(a, b) #[ 5. 12. 21. 32.]
print a / b #[ 0.2 0.33333333 0.42857143 0.5 ]
print numpy.divide(a, b) #[ 0.2 0.33333333 0.42857143 0.5 ]
print a % b #[ 1. 2. 3. 4.]
print numpy.mod(a, b) #[ 1. 2. 3. 4.]
print a**b #[ 1.00000000e+00 6.40000000e+01 2.18700000e+03 6.55360000e+04]
print numpy.power(a, b) #[ 1.00000000e+00 6.40000000e+01 2.18700000e+03 6.55360000e+04]
HackerRank Python Solution - Numpy Topic - Eye and Identity
import numpy
print numpy.identity(3) #3 is for dimension 3 X 3
#Output
[[ 1. 0. 0.]
[ 0. 1. 0.]
[ 0. 0. 1.]]
HackerRank Python Solution - Numpy Topic - Zeros and Ones
Zeros:
The zeros tool returns a new array with a given shape and type filled with 0's.
import numpy
print numpy.zeros((1,2)) #Default type is float
#Output : [[ 0. 0.]]
print numpy.zeros((1,2), dtype = numpy.int) #Type changes to int
#Output : [[0 0]]
HackerRank Python Solution - Numpy Topic - Concatenate
Concatenate:
Two or more arrays can be concatenated together using the concatenate function with a tuple of the arrays to be joined:
import numpy
array_1 = numpy.array([1,2,3])
array_2 = numpy.array([4,5,6])
array_3 = numpy.array([7,8,9])
print numpy.concatenate((array_1, array_2, array_3))
#Output
[1 2 3 4 5 6 7 8 9]
HackerRank Python Solution - Numpy Topic - Transpose and Flatten
Question 2 - Transpose and Flatten
Task
You are given an NxM integer array matrix with space-separated elements (N= rows and M= columns).
The question is to print the transpose and flatten the results.
Input Format
The first line contains the space-separated values of N and M.
The next N lines contain the space-separated elements of M columns.
HackerRank Python Solution - Numpy Topic - Arrays
Question1 - Arrays:
Task
You are given a space-separated list of numbers.
Your task is to print a reversed NumPy array with the element type float
.
Input Format
A single line of input containing space-separated numbers.
You might also like
Deploy your Django web app to Azure Web App using App Service - F1 free plan
In this post, we will look at how we can deploy our Django app using the Microsoft Azure app service - a free plan. You need an Azure accoun...