1
import heapq
class PriorityQueue:

    def __init__(self):
        self.heap = []

    def push(self, item, priority):
        pair = (priority,item)
        heapq.heappush(self.heap,pair)

    def pop(self):
       return heapq.heappop(self.heap)

    def isEmpty(self):
        return len(self.heap) == 0

    def clear(self):
        while not (self.isEmpty()):
            self.heap.pop()

    def getHeap(self):
        return self.heap

    def getLeng(self):
        return len(self.heap)

    def exists(self, item):
       return len(list(set(self.heap) & set(item)))

pq = PriorityQueue()
x = "test"
pq.push(x,1)
print pq.exists(x)     

it printed 0 when it should print 1 since intersection of a set with x and another set with x should be 1

am i overlooking things?
why is it printing 0 instead of 1?

1 Answer 1

3

You are pushing tuples of (priority,value) to the heap but want the exist method to work only on values, so you should get a value-only list/iterator out of your heap, something like this:

def exists(self, item):
   return item in (x[1] for x in self.heap)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.