Solution using heapq and custom QueueItem object with comparator fails


#1

Python solution fails using a custom QueueItem object with cmp() implemented.
Run as custom input does not consider the whole input (length :96, elements: 21). Solution is guaranteed to work, test and manual tests pass without a problem. Any tips on how to debug this?

import heapq

class QueueItem:
    def __init__(self, count, index):
       self.count = count
       self.index = index
      
    def __cmp__(self, other):
        if self.count > other.count:
            return 1
        elif self.count < other.count:
            return -1
        else:
            if self.index > other.index:
                return 1
            elif self.index < other.index:
                return -1
            else:
                return 0
        
class Solution:
    # @param A : string
    # @param B : list of strings
    # @return a list of integers
    def solve(self, A, B):
        for sen in B:
            print sen
        good_words = set(A.strip().split('_'))
        counts = []
        for item in B:
            count = len(good_words.intersection(set(item.split('_'))))
            counts.append(count)
            
        # print counts
        
        h = []
        res = []
        for i,count in enumerate(counts):
            heapq.heappush(h, QueueItem(-count, i))
            
        while h:
            res.append(heapq.heappop(h).index)
        
        return res