Solution in python vs cpp


#1
**python solution**

from collections import defaultdict

def set_(b, c, d, dic):
    dic[b].append((c, d))
    return "null"

def get_(b, d, dic):
    timestamp = d
    values = dic.get(b, "null") 
    if values != "null":
        sub_list = [x for x in values if x[1] <= d]
        if len(sub_list) != 0:
            values = max(sub_list, key = lambda x:x[1])
            values = values[0]
        else:
            values = "null"
    return values

class Solution:
    # @param A : list of integers
    # @param B : list of strings
    # @param C : list of strings
    # @param D : list of integers
    # @return a list of strings
    def solve(self, A, B, C, D):
        output = list()
        dic = defaultdict(list)
        for i, a in enumerate(A):
            if a == 1:
                output.append(set_(B[i], C[i], D[i], dic))
            else:
                output.append(get_(B[i], D[i], dic))
                
        return output

**cpp solution**

#include <list>
#include <unordered_map>
#include <tuple>

unordered_map <string, list <tuple <string, int > > > umap;

string set_(const string b, const string c, const int d){
    umap[b].push_back(make_tuple(c, d));
    return "null";
}

bool compare(const tuple<string, int>& a, const tuple<string, int>& b){
    return ((get<1>(a)) < (get<1>(b)));
}

string get_(const string b, const int d){
    int timestamp = d;
    // itr is pointer to <string, list> not just list itself
    unordered_map <string, list <tuple <string, int > > > :: iterator itr = umap.find(b);
    if (itr == umap.end()){
        return "null";
    }
    else{
        list <tuple <string, int> >& values = (itr->second);
        list <tuple <string, int> > :: iterator list_itr;
        list <tuple <string, int> > l;
        tuple <string, int> t;
        for(list_itr = values.begin(); list_itr != values.end(); list_itr++){
            t = *list_itr;
            if(get<1>(t) <= d){
                l.push_back(t);
            }
        }
        if (l.size() != 0){
            // l.sort(compare);
            // return get<0>(l.back());
            t = *max_element(l.begin(), l.end(), compare);
            return get<0>(t);
        }
        else{
            return "null";
        }
    }
}

vector<string> Solution::solve(vector<int> &A, vector<string> &B, vector<string> &C, vector<int> &D) {
    int N = A.size();
    vector<string> output(N);
    for(int i = 0; i < N; i++){
        if(A[i] ==  1){
            output[i] = set_(B[i], C[i], D[i]);
        }
        else{
            output[i] = get_(B[i], D[i]);
        }
    }
    return output;
}

Logic is same but comparatively it's less of a hassle in python.