From 9e6e62ce7fdbb7d02031e8c8bc1017fbfb45fc3e Mon Sep 17 00:00:00 2001 From: Louie Tan Date: Sat, 21 Oct 2017 01:16:27 +0800 Subject: [PATCH] Heap test cases --- utilities/python/heap.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/utilities/python/heap.py b/utilities/python/heap.py index 5bc01c6e..4bbb926f 100644 --- a/utilities/python/heap.py +++ b/utilities/python/heap.py @@ -50,3 +50,35 @@ def heappop(heap): del heap[-1] _bubble_down(heap, 0) return min_value + + + +# Example usage +heap = [3, 2, 1, 0] +heapify(heap) +print('Heap(0, 1, 2, 3):', heap) +heappush(heap, 4) +heappush(heap, 7) +heappush(heap, 6) +heappush(heap, 5) +print('Heap(0, 1, 2, 3, 4, 5, 6, 7):', heap) + +sorted_list = [heappop(heap) for _ in range(8)] +print('Heap-sorted list:', sorted_list) + +# Large test case, for randomized tests +import random + +# Heapify 0 ~ 99 +heap = list(range(100)) +random.shuffle(heap) +heapify(heap) + +# Push 100 ~ 199 in random order +new_elems = list(range(100, 200)) +random.shuffle(new_elems) +for elem in new_elems: + heappush(heap, elem) + +sorted_list = [heappop(heap) for _ in range(200)] +print(sorted_list == sorted(sorted_list))