From 8ff83ebc2cf41bd2495c05bcfcdc2e4f5d9b5d65 Mon Sep 17 00:00:00 2001 From: Suyash Dongre <109069262+Suyashd999@users.noreply.github.com> Date: Tue, 31 Oct 2023 23:23:34 +0530 Subject: [PATCH 1/2] Added doctest to heap.py --- data_structures/heap/heap.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/data_structures/heap/heap.py b/data_structures/heap/heap.py index 29bff3af07e3..3e61f5cce5bb 100644 --- a/data_structures/heap/heap.py +++ b/data_structures/heap/heap.py @@ -53,7 +53,20 @@ def __repr__(self) -> str: return str(self.h) def parent_index(self, child_idx: int) -> int | None: - """return the parent index of given child""" + """ + return the parent index of given child + + >>> h = Heap() + >>> h.build_max_heap([103, 9, 1, 7, 11, 15, 25, 201, 209, 107, 5]) + >>> h.parent_index(11) + 5 + >>> h.parent_index(209) + 104 + >>> h.parent_index(1) + 0 + >>> h.parent_index(15) + 7 + """ if child_idx > 0: return (child_idx - 1) // 2 return None From a87f7a1c9e0f493e7f43deaffdfb6bb88df54e15 Mon Sep 17 00:00:00 2001 From: Suyash Dongre <109069262+Suyashd999@users.noreply.github.com> Date: Mon, 6 Nov 2023 15:37:42 +0530 Subject: [PATCH 2/2] Update heap.py --- data_structures/heap/heap.py | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/data_structures/heap/heap.py b/data_structures/heap/heap.py index 3e61f5cce5bb..7b15e69f13ca 100644 --- a/data_structures/heap/heap.py +++ b/data_structures/heap/heap.py @@ -54,18 +54,35 @@ def __repr__(self) -> str: def parent_index(self, child_idx: int) -> int | None: """ - return the parent index of given child + returns the parent index based on the given child index >>> h = Heap() >>> h.build_max_heap([103, 9, 1, 7, 11, 15, 25, 201, 209, 107, 5]) - >>> h.parent_index(11) - 5 - >>> h.parent_index(209) - 104 + >>> h + [209, 201, 25, 103, 107, 15, 1, 9, 7, 11, 5] + + >>> h.parent_index(-1) # returns none if index is <=0 + + >>> h.parent_index(0) # returns none if index is <=0 + >>> h.parent_index(1) 0 - >>> h.parent_index(15) - 7 + >>> h.parent_index(2) + 0 + >>> h.parent_index(3) + 1 + >>> h.parent_index(4) + 1 + >>> h.parent_index(5) + 2 + >>> h.parent_index(10.5) + 4.0 + >>> h.parent_index(209.0) + 104.0 + >>> h.parent_index("Test") + Traceback (most recent call last): + ... + TypeError: '>' not supported between instances of 'str' and 'int' """ if child_idx > 0: return (child_idx - 1) // 2