Compare the Triplets

  • + 0 comments

    Here's my shortest answer (so far)…

    def compareTriplets(a, b):
        return (list(map(sum, zip(*(
          (1, 0) if i > j else (0, 1)
          for i, j in zip(a, b) if i != j))))
          or [0, 0])
    

    Also short, but done without for loop…

    def compareTriplets(a, b):
        return list(map(sum, zip(*(map(lambda i:
            (1, 0) if i[0] > i[1] else (0, 1) if i[0] < i[1] else (0, 0),
            zip(a, b))))))
    

    I'm not sure whether the for in a sequence comprehension is really considered to be the same as a for-loop, but I thought I'd try to make a solution without that keyword anyway.

    Yesterday, I had submitted this longer answer because I was sleepy…

    def compareTriplets(a, b):
        at = bt = 0
        for i in range(3):
          if a[i] > b[i]:
            at += 1
          if b[i] > a[i]:
            bt += 1
        return [at, bt]