Header Ad

HackerRank Dot and Cross problem solution in python

In this Dot and Cross problem, You are given two arrays A and B. Both have dimensions of N X M. Your task is to compute their matrix product.

HackerRank Dot and Cross solution in python


Problem solution in Python 2 programming.

import numpy
a=int(raw_input())
m=[]
n=[]
for i in range(a):
    m.append(map(int,raw_input().split()))
for i in range(a):
    n.append(map(int,raw_input().split()))
print numpy.dot(numpy.array(m),numpy.array(n))



Problem solution in Python 3 programming.

import numpy
n=int(input())
a = numpy.array([input().split() for _ in range(n)],int)
b = numpy.array([input().split() for _ in range(n)],int)
m = numpy.dot(a,b)
print (m)


Problem solution in pypy programming.

# Enter your code here. Read input from STDIN. Print output to STDOUT
import numpy
n=int(raw_input().strip())
a=[]
b=[]
for i in xrange(n):
    t=map(int,raw_input().split())
    a.append(t)
for i in xrange(n):
    t=map(int,raw_input().split())
    b.append(t)
a=numpy.matrix(a)
b=numpy.matrix(b)
print a*b


Problem solution in pypy3 programming.

# Enter your code here. Read input from STDIN. Print output to STDOUT
import numpy as np
n= int(input())
a = np.array([input().split() for _ in range(n)],dtype=np.int)
b = np.array([input().split() for _ in range(n)],dtype=np.int)

res = []
for r in range(n):
    row = [np.dot(a[r,:],b[:,i]) for i in range(n)]
    res.append(row)

res = np.array(res)
print(res)


Post a Comment

0 Comments