Skip to content

Commit

Permalink
Merge pull request #40 from nbro/iss39
Browse files Browse the repository at this point in the history
Removed check for None in the search_by_value method of the heap ds
  • Loading branch information
nbro committed Jan 21, 2017
2 parents 217a81a + 5398661 commit f12bd0c
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 16 deletions.
7 changes: 4 additions & 3 deletions ands/ds/Heap.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
Creation: July, 2015
Last update: 28/08/16
Updated: 21/01/2017
Base abstract class to represent heaps.
See `MinHeap` and `MaxHeap` if you want to instantiate heap objects.
Expand Down Expand Up @@ -151,9 +151,10 @@ def search_by_value(self, val) -> int:
If `val` and the values in this heap are not comparable,
the behaviour of this method is undefined.
By construction, HeapNode objects can't be initialized with None values,
but that field could also be set manually after creation.
**Time Complexity:** O(n)."""
if val is None:
raise ValueError("val cannot be None.")
for i, node in enumerate(self.heap):
if node.value == val:
return i
Expand Down
10 changes: 5 additions & 5 deletions tests/ds/test_MaxHeap.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@


class TestMaxHeap(unittest.TestCase):

def test_empty_heap_creation(self):
h = MaxHeap()
self.assertTrue(is_max_heap(h))
Expand Down Expand Up @@ -221,15 +220,16 @@ def test_search(self):
self.assertRaises(ValueError, h.search, None)
self.assertTrue(is_max_heap(h))

def test_search_by_value(self):
def test_search_by_value_incompatible_values(self):
ls = [28, 14, 12]
h = MaxHeap(ls)

self.assertRaises(Exception, h.search_by_value, HeapNode(14))
self.assertRaises(ValueError, h.search_by_value, None)

self.assertTrue(is_max_heap(h))

def test_search_by_value(self):
ls = [28, 14, 12]
h = MaxHeap(ls)

v = h.search_by_value(14)
self.assertIn(v, range(0, len(ls)))
self.assertEqual(h.size(), len(ls))
Expand Down
4 changes: 0 additions & 4 deletions tests/ds/test_MinHeap.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@


class TestMinHeap(unittest.TestCase):

def test_empty_heap_creation(self):
h = MinHeap()
self.assertTrue(is_min_heap(h))
Expand Down Expand Up @@ -218,9 +217,6 @@ def test_search_by_value_bad(self):
self.assertRaises(Exception, h.search_by_value, HeapNode(13))
self.assertTrue(is_min_heap(h))

self.assertRaises(ValueError, h.search_by_value, None)
self.assertTrue(is_min_heap(h))

def test_search_by_value_default(self):
ls = [28, 14, 12]
h = MinHeap(ls)
Expand Down
4 changes: 0 additions & 4 deletions tests/ds/test_MinMaxHeap.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,3 @@ def test_index_of_min_and_max(self):
self.assertEqual(h.index_of_max(7), -1)
self.assertEqual(h.index_of_min(8), -1)
self.assertEqual(h.index_of_max(8), -1)


if __name__ == "__main__":
unittest.main(verbosity=2)

0 comments on commit f12bd0c

Please sign in to comment.