Compare the Triplets

  • + 6 comments

    I like it! And even better would be not to use the zip function because the zip() function's time complexity is O(n) and iterating and comparing each element is another O(n). A better way would be to use the fact that the length of a is guaranteed to be equal to the length of b and use the length function, which has a time complexity of O(1). And in our case, we're told what the length is, which is 3. So, an improvement to your function is:

    def compareTriplets(a, b):
        a_score, b_score = 0, 0
        for i in range(3):
            a_score += a[i] > b[i]
            b_score += b[i] > a[i]
        return [a_score, b_score]