Sort by

recency

|

12 Discussions

|

  • + 0 comments

    Copilot solution:

    def mapper(record):
        record = record.strip().split(",")
        if record[0] == "Employee":
            mapReducer.emitIntermediate(record[2], ("Employee", record[1]))
        elif record[0] == "Department":
            mapReducer.emitIntermediate(record[1], ("Department", record[2]))
            
    
    def reducer(key, list_of_values):
        employee_records = [v for v in list_of_values if v[0]=="Employee"]
        department_records = [v for v in list_of_values if v[0]=="Department"]
        
        for e in employee_records:
            for d in department_records:
                mapReducer.emit((key, e[1], d[1]))
    
  • + 0 comments
    def mapper(record):
        # use SSN=Employee_ID as the joining key
        t,k,v = record.strip().split(',')
        if t[0]=='E' : k,v = v,k
        mapReducer.emitIntermediate( k, (t[0],v) )
    
    def reducer(key, values):
        # a person can have multiple nicknames
        for p in [ v for t,v in values if t=='E' ] :
            for d in [ v for t,v in values if t=='D' ] :
                mapReducer.emit( (key,p,d) )
    
  • + 0 comments

    from collections import OrderedDict 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.append(value) 
    
    def execute(self, data, mapper, reducer):
        for record in data:
            mapper(record)
    
        for key in self.intermediate:
            reducer(key, self.intermediate[key])
    
        self.result.sort()
        for item in self.result:
            print item
    

    mapReducer = MapReduce()

    def mapper(record): #Start writing the Map code here fields = record.replace('\n', '').split(',')

    if fields[0] == 'Employee':
        person_name = fields[1]
        ssn = fields[2]
        mapReducer.emitIntermediate(ssn, (fields[0], person_name))
    else:
        ssn = fields[1]
        department_name = fields[2]
        mapReducer.emitIntermediate(ssn, (fields[0], department_name))
    

    def reducer(key, list_of_values): #Start writing the Reduce code here person_names = [] department_names = [] for label, value in list_of_values: if label == 'Employee': person_names.append(value) else: department_names.append(value)

    for person_name in person_names:
        for department_name in department_names:
            mapReducer.emit((key, person_name, department_name))
    

    if name == 'main': inputData = [] for line in sys.stdin: inputData.append(line) mapReducer.execute(inputData, mapper, reducer)

  • + 0 comments

    Here is Map Reduce Advanced - Relational Join problem solution - https://www.gyangav.com/2022/11/hackerrank-map-reduce-advanced-relational-join-problem-solution.html

  • + 0 comments
    def mapper(record):
        record = record.replace('\n', '')
        data = record.split(',')
        if data[0] == 'Department':
            mapReducer.emitIntermediate(data[1], ('D', data[2]))
        elif data[0] == 'Employee':
            mapReducer.emitIntermediate(data[2], ('P', data[1]))    
        
    def reducer(key, list_of_values):
        name = [ item[1] for item in list_of_values if item[0] == 'P'][0]
        
        for item in [item[1] for item in list_of_values if item[0] == 'D']:
            mapReducer.emit((key, name, item))