• + 0 comments

    Hello, I tried an algorithm that worked on my IDE, but its wrong when i submit my solution. I think i didn't quite well understood the input format... here is the input i tried on my IDE :

    inp = [[5,4],[1,5], [3,2], [4,1], [2,4], [4,2], [1,2], [4,3], [10,3],[2,4], [5,4], [9,8]]
    print(two_robots(3,inp))
    

    I got the right output : 11, 2, 5 my algo is :

    def two_robots(m,queries):
        i = 0
    
        def min_dist(queries):
    
            n = len(queries)
            i = 1
            R1, R2 = queries[0], None
            D1, D2 = abs(queries[0][1]-queries[0][0]), 0
    
            while ((R2 is None) and (i< n-1)):
                if abs(queries[i][0] - R1[1]) > abs(queries[i+1][0] - R1[1]):
                    R2 = queries[i]
                    D2+= abs(R2[0] - R2[1])
                else:
                    D1+= abs(R1[1]- queries[i][0]) + abs(queries[i][0] - queries[i][1])
                    R1 = queries[i]
                i = i+1
            if R2 is None:
                R2 = queries[i]
                D2+= abs(R2[0] - R2[1])
            else:
                while (i < n):
                    if abs(queries[i][0]- R1[1]) > abs(queries[i][0] - R2[1]):
                        D2+= abs(R2[1]- queries[i][0]) + abs(queries[i][0] - queries[i][1])
                        R2 = queries[i]
                    else:
                        D1+= abs(R1[1]- queries[i][0]) + abs(queries[i][0] - queries[i][1])
                        R1 = queries[i]
                    i = i+1
            return D1+D2
    
        resultats = ""
        while i < m:
            n_cut = queries[0][1]
            res = min_dist(queries[1:n_cut+1])
            resultats = resultats + str(res) + "\n"
            queries = queries[n_cut+1:]
            i = i+1
        return resultats
    

    I get an error when i run this solution on hackerRank. Can anyone of you plz, give an example of the input and output, that the function, is supposed to get?? Thank you in advance