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

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

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

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 Heap queue algorithm. Go to the editor

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 Heap queue algorithm. Go to the editor

Click me to see the sample solution

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

Click me to see the sample solution

4. Write a Python function which accepts an arbitrary list and converts it to a heap using Heap queue algorithm. Go to the editor

Click me to see the sample solution

5. Write a Python program to delete the smallest element from the given Heap and then inserts a new item. Go to the editor

Click me to see the sample solution

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

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 Heap queue algorithm. Go to the editor

Click me to see the sample solution

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

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 lists of sorted and distinct integers using Heap queue algorithm. Go to the editor

Click me to see the sample solution

10. Write a Python program to get the n expensive and cheap price items from a given dataset using Heap queue algorithm. Go to the editor

Click me to see the sample solution

11. Write a Python program to merge multiple sorted inputs into a single sorted iterator (over the sorted values) using Heap queue algorithm. Go to the editor

Click me to see the sample solution

12. Given a n x n matrix where each of the rows and columns are sorted in ascending order, write a Python program to find the kth smallest element in the matrix. Go to the editor

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 Heap queue algorithm. Go to the editor

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 Heap queue algorithm. Go to the editor

Click me to see the sample solution

15. Write a Python program to check if the letters of a given string can be rearranged so that two characters that are adjacent to each other are different using Heap queue algorithm. Go to the editor

Click me to see the sample solution

16. Write a Python program which add integer numbers from the data stream to a heapq and compute the median of all elements. Use Heap queue algorithm. Go to the editor

Click me to see the sample solution

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

Click me to see the sample solution

18. Write a Python program to find the nth ugly number using Heap queue algorithm. Go to the editor

Click me to see the sample solution

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

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.

﻿

## Python: Tips of the Day

Python: While-else construct

```i = 5

while i > 1:
print("Whil-ing away!")
i -= 1
if i == 3:
break
else:
print("Finished up!")
```