Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 41331e8

Browse files
committed
Minor clean-ups for heapq.
1 parent 79cae68 commit 41331e8

1 file changed

Lines changed: 11 additions & 11 deletions

File tree

Lib/heapq.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,6 @@
127127
__all__ = ['heappush', 'heappop', 'heapify', 'heapreplace', 'merge',
128128
'nlargest', 'nsmallest', 'heappushpop']
129129

130-
from itertools import islice, count
131-
132130
def heappush(heap, item):
133131
"""Push item onto heap, maintaining the heap invariant."""
134132
heap.append(item)
@@ -378,8 +376,10 @@ def merge(*iterables):
378376
# 2 n - k compare remaining elements to top of heap
379377
# 3 k * (1 + lg2(k)) * ln(n/k) replace the topmost value on the heap
380378
# 4 k * lg2(k) - (k/2) final sort of the k most extreme values
379+
#
381380
# Combining and simplifying for a rough estimate gives:
382-
# comparisons = n + k * (1 + log(n/k)) * (1 + log(k, 2))
381+
#
382+
# comparisons = n + k * (log(k, 2) * log(n/k)) + log(k, 2) + log(n/k))
383383
#
384384
# Computing the number of comparisons for step 3:
385385
# -----------------------------------------------
@@ -391,12 +391,12 @@ def merge(*iterables):
391391
# * The probabilty times the cost gives:
392392
# (k/i) * (1 + log(k, 2))
393393
# * Summing across the remaining n-k elements gives:
394-
# sum((k/i) * (1 + log(k, 2)) for xrange(k+1, n+1))
394+
# sum((k/i) * (1 + log(k, 2)) for i in range(k+1, n+1))
395395
# * This reduces to:
396396
# (H(n) - H(k)) * k * (1 + log(k, 2))
397397
# * Where H(n) is the n-th harmonic number estimated by:
398398
# gamma = 0.5772156649
399-
# H(n) = log(n, e) + gamma + 1.0 / (2.0 * n)
399+
# H(n) = log(n, e) + gamma + 1 / (2 * n)
400400
# http://en.wikipedia.org/wiki/Harmonic_series_(mathematics)#Rate_of_divergence
401401
# * Substituting the H(n) formula:
402402
# comparisons = k * (1 + log(k, 2)) * (log(n/k, e) + (1/n - 1/k) / 2)
@@ -445,12 +445,12 @@ def nsmallest(n, iterable, key=None):
445445
# When key is none, use simpler decoration
446446
if key is None:
447447
it = iter(iterable)
448-
result = list(islice(zip(it, count()), n))
448+
result = [(elem, i) for i, elem in zip(range(n), it)]
449449
if not result:
450450
return result
451451
_heapify_max(result)
452-
order = n
453452
top = result[0][0]
453+
order = n
454454
_heapreplace = _heapreplace_max
455455
for elem in it:
456456
if elem < top:
@@ -466,8 +466,8 @@ def nsmallest(n, iterable, key=None):
466466
if not result:
467467
return result
468468
_heapify_max(result)
469-
order = n
470469
top = result[0][0]
470+
order = n
471471
_heapreplace = _heapreplace_max
472472
for elem in it:
473473
k = key(elem)
@@ -506,12 +506,12 @@ def nlargest(n, iterable, key=None):
506506
# When key is none, use simpler decoration
507507
if key is None:
508508
it = iter(iterable)
509-
result = list(islice(zip(it, count(0, -1)), n))
509+
result = [(elem, i) for i, elem in zip(range(0, -n, -1), it)]
510510
if not result:
511511
return result
512512
heapify(result)
513-
order = -n
514513
top = result[0][0]
514+
order = -n
515515
_heapreplace = heapreplace
516516
for elem in it:
517517
if top < elem:
@@ -527,8 +527,8 @@ def nlargest(n, iterable, key=None):
527527
if not result:
528528
return result
529529
heapify(result)
530-
order = -n
531530
top = result[0][0]
531+
order = -n
532532
_heapreplace = heapreplace
533533
for elem in it:
534534
k = key(elem)

0 commit comments

Comments
 (0)