Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

HackerRank Dot and Cross problem solution in python

YASH PAL, 31 July 2024

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)

coding problems python

Post navigation

Previous post
Next post
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
©2025 Programming101 | WordPress Theme by SuperbThemes