#!python

from binaryheap import BinaryMinHeap


class PriorityQueue(object):
    """PriorityQueue: a partially ordered queue with methods to enqueue items
    in priority order and to access and dequeue its highest priority item.
    Item pairs are stored in a binary min heap for its efficient operations."""

    def __init__(self):
        """Initialize this priority queue."""
        # Initialize new binary min heap to store items in this priority queue
        self.heap = BinaryMinHeap()

    def __repr__(self):
        """Return a string representation of this priority queue."""
        return 'PriorityQueue({} items, front={})'.format(self.size, self.front())

    def size(self):
        """returns the number of items in the queue"""
        return self.heap.size()

    def is_empty(self):
        """Return True if this priority queue is empty, or False otherwise."""
        return self.heap.is_empty()

    def length(self):
        """Return the number of items in this priority queue."""
        return self.heap.size()

    def enqueue(self, item, priority=0):
        """Insert the given item into this priority queue in order according to
        the given priority."""
        # Insert given item into heap in order according to given priority
        self.heap.insert((priority, item))

    def front(self):
        """Return the item at the front of this priority queue without removing
        it, or None if this priority queue is empty."""
        if self.size() == 0:
            return None
        # Return minimum item from heap
        return self.heap.get_min()[1]

    def dequeue(self):
        """Remove and return the item at the front of this priority queue,
        or raise ValueError if this priority queue is empty."""
        if self.size() == 0:
            raise ValueError('Priority queue is empty and has no front item')
        # Remove and return minimum item from heap
        return self.heap.delete_min()[1]

    def push_pop(self, item, priority):
        """Remove and return the item at the front of this priority queue,
        and insert the given item in order according to the given priority.
        This method is more efficient than calling dequeue and then enqueue."""
        if self.size() == 0:
            raise ValueError('Priority queue is empty and has no front item')
        # Replace and return minimum item from heap
        self.heap.items[0], popped_item = (priority, item), self.heap.items[0]
        self.heap._bubble_down(0)
        return popped_item[1]

def test():
    # sentance split into a list of words
    words = 'hello world today it is raining'.split(' ')
    print(' '.join(words))
    q = PriorityQueue()
    # add all the words in the sentence to the queue
    for i, word in enumerate(words):
        # have their priority go in descending order
        q.enqueue(word, 10 - i)
        assert q.size() == i + 1
    assert q.front() == 'raining'
    # dequeue the words in order in a new list
    reversed_words = []
    for _ in range(len(words)):
        reversed_words.append(q.dequeue())
    assert reversed_words == words[::-1]
    print(' '.join(reversed_words))

if __name__ == '__main__':
    test()