Mintri1199 / CS-2.1-Trees-Sorting

0 stars 0 forks source link

Feedback on heaps, priority queues, and integer sorting algorithms #3

Open Anisha7 opened 4 years ago

Anisha7 commented 4 years ago

Great work on completing these challenges. Here's some feedback to improve your code:

Mintri1199 commented 4 years ago
Anisha7 commented 4 years ago

Try these tests for your priority queue to help you debug your push_pop() method:

if __name__ == '__main__':
    test_priority_queue()
    PQ = PriorityQueue()
    assert(PQ.is_empty())
    assert(PQ.length() == 0)
    PQ.enqueue("apple", 2)
    assert(PQ.length() == 1)
    assert(PQ.front() == "apple")
    assert(PQ.length() == 1)
    PQ.enqueue("orange", 0)
    assert(PQ.length() == 2)
    assert(PQ.front() == "orange")
    assert(PQ.length() == 2)
    assert(PQ.dequeue() == "orange")
    assert(PQ.length() == 1)
    assert(PQ.push_pop("banana", 4) == "apple")
    assert(PQ.length() == 1)
    assert(PQ.front() == "banana")
    assert(PQ.length() == 1)
    print("PASSED")

You need to return the item, not the tuple.

Mintri1199 commented 4 years ago

Oh ok, sorry I overlooked it. I just needed to index the item in the tuple like so return self.heap.replace_min((priority, item))[1]