From c7c7ce7bf6883b74fecc580385f5122c182b9b46 Mon Sep 17 00:00:00 2001 From: Yuting Liu Date: Fri, 3 Mar 2023 17:14:56 -0800 Subject: [PATCH 1/2] Add max heap version of heappush --- Lib/heapq.py | 5 +++++ Lib/test/test_heapq.py | 9 ++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/Lib/heapq.py b/Lib/heapq.py index 2fd9d1ff4bf827..48840dfad2ff94 100644 --- a/Lib/heapq.py +++ b/Lib/heapq.py @@ -188,6 +188,11 @@ def _heappop_max(heap): return returnitem return lastelt +def heappush_pop(heap, item): + """Maxheap version of a heappush.""" + heap.append(item) + _siftdown_max(heap, 0, len(heap)-1) + def _heapreplace_max(heap, item): """Maxheap version of a heappop followed by a heappush.""" returnitem = heap[0] # raises appropriate IndexError if heap is empty diff --git a/Lib/test/test_heapq.py b/Lib/test/test_heapq.py index 1aa8e4e289730d..a38f8d8db8f491 100644 --- a/Lib/test/test_heapq.py +++ b/Lib/test/test_heapq.py @@ -14,7 +14,7 @@ # _heapq.nlargest/nsmallest are saved in heapq._nlargest/_smallest when # _heapq is imported, so check them there func_names = ['heapify', 'heappop', 'heappush', 'heappushpop', 'heapreplace', - '_heappop_max', '_heapreplace_max', '_heapify_max'] + '_heappop_max', '_heappush_max', '_heapreplace_max', '_heapify_max'] class TestModules(TestCase): def test_py_functions(self): @@ -160,6 +160,13 @@ def test_heappop_max(self): self.assertEqual(self.module._heappop_max(h), 3) self.assertEqual(self.module._heappop_max(h), 2) + def test_heappush_max(self): + h = [3, 2] + self.module._heappush_max(h, 3) + self.assertEqual(h[0], 3) + self.module._heappush_max(h, 5) + self.assertEqual(h[0], 5) + def test_heapsort(self): # Exercise everything with repeated heapsort checks for trial in range(100): From 7726c5a7a91cf69716a158c2b8e7a0e3de1e0e23 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sun, 5 Mar 2023 01:55:15 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2023-03-05-01-55-14.gh-issue-102430.0lHPG8.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2023-03-05-01-55-14.gh-issue-102430.0lHPG8.rst diff --git a/Misc/NEWS.d/next/Library/2023-03-05-01-55-14.gh-issue-102430.0lHPG8.rst b/Misc/NEWS.d/next/Library/2023-03-05-01-55-14.gh-issue-102430.0lHPG8.rst new file mode 100644 index 00000000000000..9cc9c06465ba05 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-03-05-01-55-14.gh-issue-102430.0lHPG8.rst @@ -0,0 +1 @@ +Heappush operation is missing for max heap version in heapq