# 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.*]

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

**20.** Write a Python program to combine two given sorted lists using heapq module. Go to the editor

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

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

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

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

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

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 size of the queue. Go to the editor

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 whether a queue is empty or not. Go to the editor

Sample Output:

True

False

Click me to see the sample solution

**28.** Write a Python program to create a FIFO queue. Go to the editor

Sample Output:

0 1 2 3

Click me to see the sample solution

**29.** Write a Python program to create a LIFO queue. Go to the editor

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

**Calling an external command from Python:**

The subprocess module in the standard library:

Example:

import subprocess subprocess.run(["ls", "-l"])

The advantage of subprocess vs. system is that it is more flexible (you can get the stdout, stderr, the "real" status code, better error handling, etc...).

The official documentation recommends the subprocess module over the alternative os.system():

The subprocess module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using this function [os.system()].

The Replacing Older Functions with the subprocess Module section in the subprocess documentation may have some helpful recipes.

For versions of Python before 3.5, use call:

import subprocess subprocess.call(["ls", "-l"])

Ref: https://bit.ly/2ANeuPR

**New Content published on w3resource:**- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- React - JavaScript Library
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework