Skip to content

Commit e84ebce

Browse files
committed
Tweaks to autoresizelist, added build_heap, but it has a bug (as is being caught by the unit test).
1 parent c2f9c4e commit e84ebce

File tree

4 files changed

+66
-31
lines changed

4 files changed

+66
-31
lines changed

autoresizelist.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ def __setitem__(self, key, value):
1919
self._data[key] = value
2020

2121
def __getitem__(self, key):
22-
if key >= len(self._data):
23-
self._data += [self.fill] * (key - len(self._data) + 1)
22+
#if key >= len(self._data):
23+
# self._data += [self.fill] * (key - len(self._data) + 1)
2424
return self._data[key]
2525

2626
def __delitem__(self, key):
@@ -35,6 +35,12 @@ def __eq__(self, other):
3535
def __len__(self):
3636
return len(self._data)
3737

38+
def append(self, item):
39+
self._data.append(item)
40+
41+
def prepend(self, item):
42+
self._data = [item] + self._data
43+
3844
if __name__ == "__main__":
3945
import unittest
4046
testsuite = unittest.TestLoader().discover('test', pattern="*autoresizelist*")

heap.py

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,17 @@ class HeapType(Enum):
1515

1616
class Heap:
1717
def __init__(self, initial_data=None, heap_type=HeapType.maxheap):
18-
if initial_data is None:
19-
self.data = AutoResizeList()
20-
else:
21-
self.data = AutoResizeList(initial_data)
22-
self.build_heap(initial_data)
23-
2418
self.heap_type = heap_type
2519
if heap_type == HeapType.maxheap:
2620
self.comparator = lambda x, y: x > y
2721
else:
2822
self.comparator = lambda x, y: x < y
2923

30-
self.size = len(self.data)
24+
self.data = AutoResizeList()
25+
if initial_data is not None:
26+
self.build_heap(initial_data)
27+
28+
self._size = len(self.data)
3129

3230
def _left_child(self, index):
3331
return 2*index + 1
@@ -46,7 +44,8 @@ def _swap(self, i1, i2):
4644

4745
def build_heap(self, initial_data):
4846
for i in initial_data:
49-
self.data.append(i)
47+
self.data.prepend(i)
48+
self.heap_down(0)
5049

5150
def heap_up(self, index):
5251
# If we are at the root, return - we are done
@@ -63,8 +62,14 @@ def heap_up(self, index):
6362
def heap_down(self, index):
6463
left_index = self._left_child(index)
6564
right_index = self._right_child(index)
66-
left = self.data[left_index]
67-
right = self.data[right_index]
65+
try:
66+
left = self.data[left_index]
67+
except IndexError:
68+
left = None
69+
try:
70+
right = self.data[right_index]
71+
except IndexError:
72+
right = None
6873

6974
# Find the largest child
7075
largest_child = left
@@ -84,8 +89,8 @@ def heap_down(self, index):
8489
self.heap_down(largest_child_index)
8590

8691
def push(self, item):
87-
insert_index = self.size # Insert at the end
88-
self.size += 1
92+
insert_index = self._size # Insert at the end
93+
self._size += 1
8994

9095
self.data[insert_index] = item
9196
self.heap_up(insert_index)
@@ -103,19 +108,21 @@ def pop(self):
103108
item = self.data[0]
104109

105110
# Move the bottom-most, right-most item to the root
106-
self.data[0] = self.data[self.size-1]
107-
self.data[self.size-1] = None
108-
self.size -= 1
111+
self.data[0] = self.data[self._size-1]
112+
self.data[self._size-1] = None
113+
self._size -= 1
109114

110115
self.heap_down(0)
111116

112117
return item
113118

119+
def size(self):
120+
return self._size
121+
114122
def __repr__(self):
115123
return str(self.data)
116124

117125
if __name__ == "__main__":
118126
import unittest
119127
testsuite = unittest.TestLoader().discover('test', pattern="*heap*")
120128
unittest.TextTestRunner(verbosity=1).run(testsuite)
121-

test/test_autoresizelist.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ def testSet(self):
1919

2020
def testGet(self):
2121
c = AutoResizeList([], fill=100)
22-
self.assertEqual(c[10], 100)
22+
c[10] = 1
23+
self.assertEqual(c[10], 1)
24+
self.assertEqual(c[9], 100)
2325

2426
def testDel(self):
2527
d = AutoResizeList([0, 1, 2])
@@ -28,14 +30,19 @@ def testDel(self):
2830
self.assertEqual(d._data, [0, 1, 2, 4])
2931

3032
def testEqual(self):
31-
a = AutoResizeList([1,2,3])
32-
b = AutoResizeList([1,2,3])
33+
a = AutoResizeList([1, 2, 3])
34+
b = AutoResizeList([1, 2, 3])
3335
self.assertEqual(a, b)
3436

3537
def testLen(self):
36-
a = AutoResizeList([1,2,3])
38+
a = AutoResizeList([1, 2, 3])
3739
self.assertEqual(len(a), 3)
3840

41+
def testPrepend(self):
42+
a = AutoResizeList([1, 2, 3])
43+
a.prepend(0)
44+
self.assertEqual(0, a._data[0])
45+
3946
if __name__ == '__main__':
4047
unittest.main()
4148

test/test_heap.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
import unittest
22
import sys
33
import os.path
4+
import random
45

56
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir)))
67
import heap
78

89

9-
class HeapTest(unittest.TestCase):
10+
def is_heap(h):
11+
sorted_data = list(sorted(filter(lambda x: x is not None, h.data), reverse=True))
12+
pop_order = [h.pop() for _ in range(h.size())]
13+
print("\nsorted data: {0}\npop_order: {1}".format(sorted_data, pop_order))
14+
return sorted_data == pop_order
15+
1016

17+
class HeapTest(unittest.TestCase):
1118
def testMaxHeapPushPop(self):
1219
# Test push
1320
h = heap.Heap()
@@ -35,6 +42,14 @@ def testMaxHeapPushPop(self):
3542
self.assertEqual(h.pop(), 9)
3643
self.assertEqual(h.pop(), 7)
3744

45+
# Larger test
46+
data = [93, 89, 91, 84, 87, 54, 71, 62, 75, 35, 43, 50, 77, 27, 14, 42, 13, 35, 10, 4]
47+
h2 = heap.Heap()
48+
for d in data:
49+
h2.push(d)
50+
51+
self.assertTrue(is_heap(h2))
52+
3853
def testMinHeapPushPop(self):
3954
# Test Push
4055
h = heap.Heap([], heap.HeapType.minheap)
@@ -65,14 +80,14 @@ def testMinHeapPushPop(self):
6580
self.assertEqual(h.pop(), -10)
6681

6782
def testMaxHeapBuild(self):
68-
pass
69-
#data = [6, 1, 4, 9, 8, 2, 3]
70-
#h = heap.Heap(data, heap.HeapType.maxheap)
71-
#self.assertEqual(h.pop(), 9)
72-
#self.assertEqual(h.pop(), 8)
73-
#h.push(15)
74-
#h.push(0)
75-
#self.assertEqual(h.pop(), 15)
83+
data = [98, 45, 69, 85, 34, 66, 10, 2, 11, 3, 21, 30, 91, 32, 55, 74, 93, 17, 67, 19]
84+
h = heap.Heap(data)
85+
self.assertTrue(is_heap(h))
86+
87+
# data2 = [random.randint(1, 100) for _ in range(20)]
88+
# print("\n\ndata: {0}".format(data2))
89+
# h3 = heap.Heap(data2[:])
90+
# self.assertTrue(is_heap(h3))
7691

7792
if __name__ == '__main__':
7893
unittest.main()

0 commit comments

Comments
 (0)