We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
from collections import OrderedDict
import itertools
import sys
class MapReduce:
def init(self):
self.intermediate = OrderedDict()
self.result = []
def emitIntermediate(self, key, value):
self.intermediate.setdefault(key, [])
self.intermediate[key].append(value)
def emit(self, value):
self.result[value[0]][value[1]] = value[2]
def execute(self, matrix1, matrix2, mapper, reducer):
n = len(matrix1)
m = len(matrix2[0])
for i in xrange(0,n):
self.result.append([0]*m)
mapper(matrix1, matrix2)
for key in self.intermediate:
reducer(key, self.intermediate[key])
for i in xrange(0,n):
row = ""
for j in xrange(0,m):
row += str(self.result[i][j]) + " "
print row
mapReducer = None
def mapper(matrix1, matrix2):
#Start writing the Map code here
n = len(matrix1)
k = len(matrix1[0])
m = len(matrix2[0])
for i in xrange(n):
for j in xrange(m):
for p in xrange(k):
mapReducer.emitIntermediate((i, j), (matrix1[i][p], matrix2[p][j]))
def reducer(key, list_of_values):
#Start writing the Reduce code here
mapReducer.emit((key[0], key[1], sum(itertools.starmap(lambda x, y: x * y, list_of_values))))
if name == 'main':
testcases = int(sys.stdin.readline().strip())
for t in xrange(0,testcases):
mapReducer = MapReduce()
dimensions = sys.stdin.readline().strip().split(" ")
row = int(dimensions[0])
column = int(dimensions[1])
matrix1 = []
matrix2 = []
for i in range(0, row):
read_row = sys.stdin.readline().strip()
matrix1.append([])
row_elems = read_row.strip().split()
for j in range(0, len(row_elems)):
matrix1[i].append(int(row_elems[j]))
dimensions = sys.stdin.readline().strip().split(" ")
row = int(dimensions[0])
column = int(dimensions[1])
for i in range(0, row):
read_row = sys.stdin.readline().strip()
matrix2.append([])
row_elems = read_row.strip().split()
for j in range(0, len(row_elems)):
matrix2[i].append(int(row_elems[j]))
mapReducer.execute(matrix1, matrix2, mapper, reducer)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Map Reduce Advanced - Matrix Multiplication
You are viewing a single comment's thread. Return to all comments →
!/usr/bin/env python2.7
from collections import OrderedDict import itertools import sys
class MapReduce: def init(self): self.intermediate = OrderedDict() self.result = []
mapReducer = None
def mapper(matrix1, matrix2): #Start writing the Map code here n = len(matrix1) k = len(matrix1[0]) m = len(matrix2[0])
def reducer(key, list_of_values): #Start writing the Reduce code here mapReducer.emit((key[0], key[1], sum(itertools.starmap(lambda x, y: x * y, list_of_values))))
if name == 'main': testcases = int(sys.stdin.readline().strip()) for t in xrange(0,testcases): mapReducer = MapReduce() dimensions = sys.stdin.readline().strip().split(" ") row = int(dimensions[0]) column = int(dimensions[1]) matrix1 = [] matrix2 = [] for i in range(0, row): read_row = sys.stdin.readline().strip() matrix1.append([]) row_elems = read_row.strip().split() for j in range(0, len(row_elems)): matrix1[i].append(int(row_elems[j])) dimensions = sys.stdin.readline().strip().split(" ") row = int(dimensions[0]) column = int(dimensions[1]) for i in range(0, row): read_row = sys.stdin.readline().strip() matrix2.append([]) row_elems = read_row.strip().split() for j in range(0, len(row_elems)): matrix2[i].append(int(row_elems[j])) mapReducer.execute(matrix1, matrix2, mapper, reducer)