Python: heap queue algorithm - Exercises, Practice, Solution
This resource offers a total of 145 Python heap queue algorithm problems for practice. It includes 29 main exercises, each accompanied by solutions, detailed explanations, and four related problems.
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.
[An Editor is available at the bottom of the page to write and execute the scripts.]
1. Three Largest Integers
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. Three Smallest Integers
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. Heapsort Implementation
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. List to Heap Conversion
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. Heap Update (Delete and Insert)
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. Heapsort Ascending Order
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. Kth Largest Element
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. Maximum Product of Three Numbers
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. Top K Frequent Integers
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. Price Extremes from Dataset
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. Merge Sorted Inputs
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. Kth Smallest in Sorted Matrix
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. Nth Super Ugly Number
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. K Most Frequent Words
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. Rearrange String (No Adjacent Duplicates)
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. Dynamic Median Finder
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. K Pairs from Two Sorted Arrays
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. Nth Ugly Number
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. Heap Tree Display
Write a Python program to print a heap as a tree-like data structure.
Click me to see the sample solution
20. Merge Two Sorted Lists
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. Push Three Items to Heap
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. Push & Pop Smallest
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. Heappushpop Operation
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. Heapsort via Heap Operations
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. Extreme Items Extraction
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. Queue Creation and Display
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. Queue Emptiness Check
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. FIFO Queue Creation
Write a Python program to create a FIFO queue.
Sample Output:
0 1 2 3
Click me to see the sample solution
29. LIFO Queue (Stack) Creation
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