Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
41331e81
Kaydet (Commit)
41331e81
authored
May 26, 2014
tarafından
Raymond Hettinger
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Minor clean-ups for heapq.
üst
79cae680
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
11 additions
and
11 deletions
+11
-11
heapq.py
Lib/heapq.py
+11
-11
No files found.
Lib/heapq.py
Dosyayı görüntüle @
41331e81
...
...
@@ -127,8 +127,6 @@ From all times, sorting has always been a Great Art! :-)
__all__
=
[
'heappush'
,
'heappop'
,
'heapify'
,
'heapreplace'
,
'merge'
,
'nlargest'
,
'nsmallest'
,
'heappushpop'
]
from
itertools
import
islice
,
count
def
heappush
(
heap
,
item
):
"""Push item onto heap, maintaining the heap invariant."""
heap
.
append
(
item
)
...
...
@@ -378,8 +376,10 @@ def merge(*iterables):
# 2 n - k compare remaining elements to top of heap
# 3 k * (1 + lg2(k)) * ln(n/k) replace the topmost value on the heap
# 4 k * lg2(k) - (k/2) final sort of the k most extreme values
#
# Combining and simplifying for a rough estimate gives:
# comparisons = n + k * (1 + log(n/k)) * (1 + log(k, 2))
#
# comparisons = n + k * (log(k, 2) * log(n/k)) + log(k, 2) + log(n/k))
#
# Computing the number of comparisons for step 3:
# -----------------------------------------------
...
...
@@ -391,12 +391,12 @@ def merge(*iterables):
# * The probabilty times the cost gives:
# (k/i) * (1 + log(k, 2))
# * Summing across the remaining n-k elements gives:
# sum((k/i) * (1 + log(k, 2)) for
x
range(k+1, n+1))
# sum((k/i) * (1 + log(k, 2)) for
i in
range(k+1, n+1))
# * This reduces to:
# (H(n) - H(k)) * k * (1 + log(k, 2))
# * Where H(n) is the n-th harmonic number estimated by:
# gamma = 0.5772156649
# H(n) = log(n, e) + gamma + 1
.0 / (2.0
* n)
# H(n) = log(n, e) + gamma + 1
/ (2
* n)
# http://en.wikipedia.org/wiki/Harmonic_series_(mathematics)#Rate_of_divergence
# * Substituting the H(n) formula:
# 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):
# When key is none, use simpler decoration
if
key
is
None
:
it
=
iter
(
iterable
)
result
=
list
(
islice
(
zip
(
it
,
count
()),
n
))
result
=
[(
elem
,
i
)
for
i
,
elem
in
zip
(
range
(
n
),
it
)]
if
not
result
:
return
result
_heapify_max
(
result
)
order
=
n
top
=
result
[
0
][
0
]
order
=
n
_heapreplace
=
_heapreplace_max
for
elem
in
it
:
if
elem
<
top
:
...
...
@@ -466,8 +466,8 @@ def nsmallest(n, iterable, key=None):
if
not
result
:
return
result
_heapify_max
(
result
)
order
=
n
top
=
result
[
0
][
0
]
order
=
n
_heapreplace
=
_heapreplace_max
for
elem
in
it
:
k
=
key
(
elem
)
...
...
@@ -506,12 +506,12 @@ def nlargest(n, iterable, key=None):
# When key is none, use simpler decoration
if
key
is
None
:
it
=
iter
(
iterable
)
result
=
list
(
islice
(
zip
(
it
,
count
(
0
,
-
1
)),
n
))
result
=
[(
elem
,
i
)
for
i
,
elem
in
zip
(
range
(
0
,
-
n
,
-
1
),
it
)]
if
not
result
:
return
result
heapify
(
result
)
order
=
-
n
top
=
result
[
0
][
0
]
order
=
-
n
_heapreplace
=
heapreplace
for
elem
in
it
:
if
top
<
elem
:
...
...
@@ -527,8 +527,8 @@ def nlargest(n, iterable, key=None):
if
not
result
:
return
result
heapify
(
result
)
order
=
-
n
top
=
result
[
0
][
0
]
order
=
-
n
_heapreplace
=
heapreplace
for
elem
in
it
:
k
=
key
(
elem
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment