HackerRank Python Solution - Numpy Topic - Dot and Cross

Dot:

The dot tool returns the dot product of two arrays.

import numpy

A = numpy.array([ 1, 2 ])
B = numpy.array([ 3, 4 ])

print numpy.dot(A, B)       #Output : 11
Cross:

The cross tool returns the cross product of two arrays.

import numpy

A = numpy.array([ 1, 2 ])
B = numpy.array([ 3, 4 ])

print numpy.cross(A, B)     #Output : -2
Task:

You are given two arrays A and B. Both have dimensions of N x N. Your task is to compute their matrix product.

Input Format: 

The first line contains the integer N. The next N lines contain N space-separated integers of array A. The following N lines contain N space-separated integers of array B.

Output Format:

Print the matrix multiplication of A and B.

Sample Input:

2
1 2
3 4
1 2
3 4
Sample Output:

[[ 7 10]
 [15 22]]
Solution:

import numpy as np

n = int(input())

arr = [list(map(int,input().split())) for _ in range(n)]
arr2 = [list(map(int,input().split())) for _ in range(n)]
print(np.dot(arr,arr2))
Disclaimer: The problem statement is given by hackerrank.com but the solution is generated by the Geek4Tutorial admin. If there is any concern regarding this post or website, please contact us using the contact form. Thank you!

No comments:

Post a Comment

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...