# Python: heap queue algorithm - Exercises, Practice, Solution

## Python heap queue algorithm [29 exercises with solution ]

[An editor is available at the bottom of the page to write and execute the scripts. Go to the editor]

Heaps are binary trees for which every parent node has a value less than or equal to any of its children.
Here are some exercises of heap queue algorithm. 1. Write a Python program to find the three largest integers from a given list of numbers using the heap queue algorithm.

Click me to see the sample solution

2. Write a Python program to find the three smallest integers from a given list of numbers using the heap queue algorithm.

Click me to see the sample solution

3. Write a Python program to implement heapsort by pushing all values onto a heap and then popping off the smallest values one at a time.

Click me to see the sample solution

4. Write a Python function that accepts an arbitrary list and converts it to a heap using the heap queue algorithm.

Click me to see the sample solution

5. Write a Python program that deletes the smallest element from a heap and then inserts a new item.

Click me to see the sample solution

6. Write a Python program to sort a given list of elements in ascending order using the heap queue algorithm.

Click me to see the sample solution

7. Write a Python program to find the kth (1 <= k <= array's length) largest element in an unsorted array using the heap queue algorithm.

Click me to see the sample solution

8. Write a Python program to compute the maximum product of three numbers in a given array of integers using the heap queue algorithm.

Click me to see the sample solution

9. Write a Python program to find the top k integers that occur the most frequently from a given list of sorted and distinct integers using the heap queue algorithm.

Click me to see the sample solution

10. Write a Python program to get the most expensive and least expensive items from a given dataset using the heap queue algorithm.

Click me to see the sample solution

11. Write a Python program that merges multiple sorted inputs into a single sorted iterator (over the sorted values) using the heap queue algorithm.

Click me to see the sample solution

12. Given a n x n matrix where each of the rows and columns is sorted in ascending order, write a Python program to find the kth smallest element in the matrix using the heap queue algorithm.

Click me to see the sample solution

13. Write a Python program to find the nth super ugly number from a given prime list of size k using the heap queue algorithm.

Click me to see the sample solution

14. Write a Python program to get the k most frequent elements from a given non-empty list of words using the heap queue algorithm

Click me to see the sample solution

15. Write a Python program to check if the letters in a given string can be rearranged. This is to make sure that two characters that are adjacent to each other are different using the heap queue algorithm.

Click me to see the sample solution

16. Write a Python program that adds integer numbers from the data stream to a heapq and computes the median of all elements. Use the heap queue algorithm.

Click me to see the sample solution

17. You have two integer arrays sorted in ascending order and an integer k. Write a Python program to find k number of pairs (u, v) which consist of one element from the first array and one element from the second array using the heap queue algorithm.

Click me to see the sample solution

18. Write a Python program to find the nth ugly number using the heap queue algorithm.
Sample Output:
7th Ugly number:
8
10th Ugly number:
12
Click me to see the sample solution

19. Write a Python program to print a heap as a tree-like data structure.

Click me to see the sample solution

20. Write a Python program to combine two sorted lists using the heapq module.
Sample Output:
Original sorted lists:
[1, 3, 5, 7, 9, 11]
[0, 2, 4, 6, 8, 10]
After merging the said two sorted lists:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

Click me to see the sample solution

21. Write a Python program to push three items into the heap and print the items from the heap.
Sample Output:
('V', 1)
('V', 2)
('V', 3)

Click me to see the sample solution

22. Write a Python program to push three items into a heap and return the smallest item from the heap. Also, pop and return the smallest item from the heap.
Sample Output:
Items in the heap:
('V', 1)
('V', 3)
('V', 2)
----------------------
The smallest item in the heap:
('V', 1)
----------------------
Pop the smallest item in the heap:
('V', 2)
('V', 3)

Click me to see the sample solution

23. Write a Python program to push an item on the heap, then pop and return the smallest item from the heap.
Sample Output:
Items in the heap:
('V', 1)
('V', 3)
('V', 2)
----------------------
Using heappushpop push item on the heap and return the smallest item.
('V', 2)
('V', 3)
('V', 6)

Click me to see the sample solution

24. Write a Python program to create a heapsort, pushing all values onto a heap and then popping off the smallest values one at a time.
Sample Output:
[10, 20, 20, 40, 50, 50, 60, 70, 80, 90, 100]

Click me to see the sample solution

25. Write a Python program to get the two largest and three smallest items from a dataset.
Sample Output:
[100, 90] [10, 20, 20]

Click me to see the sample solution

26. Write a Python program to create a queue and display all the members and the size of the queue.
Sample Output:
Members of the queue:
0 1 2 3
Size of the queue:
4

Click me to see the sample solution

27. Write a Python program to find out whether a queue is empty or not.
Sample Output:
True
False

Click me to see the sample solution

28. Write a Python program to create a FIFO queue.
Sample Output:
0 1 2 3

Click me to see the sample solution

29. Write a Python program to create a LIFO queue.
Sample Output:
3 2 1 0

Click me to see the sample solution

Python Code Editor:

More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.

Test your Python skills with w3resource's quiz

﻿

## Python: Tips of the Day

Combining Lists Using Zip:

• Takes multiple collections and returns a new collection.
• The new collection contains items where each item contains one element from each input collection.
• It allows us to transverse multiple collections at the same time.
```name = 'abcdef'
suffix = [1,2,3,4,5,6]
zip(name, suffix)
--> returns (a,1),(b,2),(c,3),(d,4),(e,5),(f,6)
```

We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook