mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +00:00
Added new heapreplace(heap, item) function, to pop (and return) the
currently-smallest value, and add item, in one gulp. See the second N-Best algorithm in the test suite for a natural use.
This commit is contained in:
parent
657fe38241
commit
0cd53a6c37
2 changed files with 20 additions and 3 deletions
|
@ -2,7 +2,7 @@
|
|||
|
||||
from test.test_support import verify, vereq, verbose, TestFailed
|
||||
|
||||
from heapq import heappush, heappop, heapify
|
||||
from heapq import heappush, heappop, heapify, heapreplace
|
||||
import random
|
||||
|
||||
def check_invariant(heap):
|
||||
|
@ -68,8 +68,7 @@ def test_main():
|
|||
heapify(heap)
|
||||
for item in data[10:]:
|
||||
if item > heap[0]: # this gets rarer the longer we run
|
||||
heappop(heap) # we know heap[0] isn't in best 10 anymore
|
||||
heappush(heap, item)
|
||||
heapreplace(heap, item)
|
||||
vereq(list(heapiter(heap)), data_sorted[-10:])
|
||||
# Make user happy
|
||||
if verbose:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue