Skip to content

Commit 4e6bb9a

Browse files
committed
Deployed bd13a27 with MkDocs version: 1.0.4
1 parent a7d5de3 commit 4e6bb9a

File tree

6 files changed

+101
-27
lines changed

6 files changed

+101
-27
lines changed

16_优先级队列/priority_queue.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,10 @@ def test_priority_queue():
132132

133133

134134
def test_buildin_PriorityQueue(): # python3
135-
# https://pythonguides.com/priority-queue-in-python/
135+
"""
136+
测试内置的 PriorityQueue
137+
https://pythonguides.com/priority-queue-in-python/
138+
"""
136139
from queue import PriorityQueue
137140
q = PriorityQueue()
138141
q.put((10, 'Red balls'))
@@ -145,6 +148,9 @@ def test_buildin_PriorityQueue(): # python3
145148

146149

147150
def test_buildin_heapq_as_PriorityQueue():
151+
"""
152+
测试使用 heapq 实现优先级队列,保存一个 tuple 比较元素(tuple第一个元素是优先级)
153+
"""
148154
import heapq
149155
s_roll = []
150156
heapq.heappush(s_roll, (4, "Tom"))
@@ -172,6 +178,10 @@ def __str__(self):
172178

173179

174180
def test_heap_item():
181+
"""
182+
测试使用 Item 类实现优先级队列,因为 heapq 内置使用的是小于运算法,
183+
重写魔术 < 比较方法即可实现
184+
"""
175185
import heapq
176186
pq = []
177187
heapq.heappush(pq, Item('c', 3))

19_python内置常用算法和数据结构/builtins/index.html

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,9 @@
176176
<li class="toctree-l2"><a href="#_2">链表题目调试函数</a></li>
177177

178178

179+
<li class="toctree-l2"><a href="#_3">内置库实现优先级队列的三种方式</a></li>
180+
181+
179182
</ul>
180183
</li>
181184

@@ -291,6 +294,7 @@ <h1 id="_1">一些坑</h1>
291294
<li>正确初始化一个二维数组:<code>dp = [[0 for _ in range(col)] for _ in range(row)]</code>,不要用 <code>dp = [[0] * n] * m</code>, 否则里边都
292295
引用的同一个 list,修改一个都会变</li>
293296
<li>python在数值范围建议用:<code>MAXINT = 2**63-1; MININT = -2**63</code> 。因为 python2 sys.maxint 和 python3 sys.maxsize 不统一</li>
297+
<li>优先级队列:使用内置的 heapq ,定义一个 Item 类实现"小于" 魔术方法就可以实现</li>
294298
</ul>
295299
<h1 id="_2">链表题目调试函数</h1>
296300
<pre><code class="language-py"># 编写链表题目经常用到的一些通用函数和调试函数,定义等,方便代码调试
@@ -345,6 +349,66 @@ <h1 id="_2">链表题目调试函数</h1>
345349
res += &quot;nil&quot;
346350
print(res)
347351
</code></pre>
352+
<h1 id="_3">内置库实现优先级队列的三种方式</h1>
353+
<pre><code class="language-py">def test_buildin_PriorityQueue(): # python3
354+
&quot;&quot;&quot;
355+
测试内置的 PriorityQueue
356+
https://pythonguides.com/priority-queue-in-python/
357+
&quot;&quot;&quot;
358+
from queue import PriorityQueue
359+
q = PriorityQueue()
360+
q.put((10, 'Red balls'))
361+
q.put((8, 'Pink balls'))
362+
q.put((5, 'White balls'))
363+
q.put((4, 'Green balls'))
364+
while not q.empty():
365+
item = q.get()
366+
print(item)
367+
368+
369+
def test_buildin_heapq_as_PriorityQueue():
370+
&quot;&quot;&quot;
371+
测试使用 heapq 实现优先级队列,保存一个 tuple 比较元素(tuple第一个元素是优先级)
372+
&quot;&quot;&quot;
373+
import heapq
374+
s_roll = []
375+
heapq.heappush(s_roll, (4, &quot;Tom&quot;))
376+
heapq.heappush(s_roll, (1, &quot;Aruhi&quot;))
377+
heapq.heappush(s_roll, (3, &quot;Dyson&quot;))
378+
heapq.heappush(s_roll, (2, &quot;Bob&quot;))
379+
while s_roll:
380+
deque_r = heapq.heappop(s_roll)
381+
print(deque_r)
382+
383+
384+
# python3 没有了 __cmp__ 魔法函数 https://stackoverflow.com/questions/8276983/why-cant-i-use-the-method-cmp-in-python-3-as-for-python-2
385+
class Item:
386+
def __init__(self, key, weight):
387+
self.key, self.weight = key, weight
388+
389+
def __lt__(self, other): # 看其来 heapq 实现只用了 小于 比较,这里定义了就可以 push 一个 item 类
390+
return self.weight &lt; other.weight
391+
392+
def __eq__(self, other):
393+
return self.weight == other.weight
394+
395+
def __str__(self):
396+
return '{}:{}'.format(self.key,self.weight)
397+
398+
399+
def test_heap_item():
400+
&quot;&quot;&quot;
401+
测试使用 Item 类实现优先级队列,因为 heapq 内置使用的是小于运算法,
402+
重写魔术 &lt; 比较方法即可实现
403+
&quot;&quot;&quot;
404+
import heapq
405+
pq = []
406+
heapq.heappush(pq, Item('c', 3))
407+
heapq.heappush(pq, Item('a', 1))
408+
heapq.heappush(pq, Item('b', 2))
409+
while pq:
410+
print(heapq.heappop(pq))
411+
</code></pre>
348412

349413
</div>
350414
</div>

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,5 +599,5 @@ <h2 id="_22">本电子书制作和写作方式</h2>
599599

600600
<!--
601601
MkDocs version : 1.0.4
602-
Build Date UTC : 2022-01-24 15:29:58
602+
Build Date UTC : 2022-01-26 13:16:15
603603
-->

search/search_index.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

sitemap.xml

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,122 +2,122 @@
22
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
33
<url>
44
<loc>None</loc>
5-
<lastmod>2022-01-24</lastmod>
5+
<lastmod>2022-01-26</lastmod>
66
<changefreq>daily</changefreq>
77
</url>
88
<url>
99
<loc>None</loc>
10-
<lastmod>2022-01-24</lastmod>
10+
<lastmod>2022-01-26</lastmod>
1111
<changefreq>daily</changefreq>
1212
</url>
1313
<url>
1414
<loc>None</loc>
15-
<lastmod>2022-01-24</lastmod>
15+
<lastmod>2022-01-26</lastmod>
1616
<changefreq>daily</changefreq>
1717
</url>
1818
<url>
1919
<loc>None</loc>
20-
<lastmod>2022-01-24</lastmod>
20+
<lastmod>2022-01-26</lastmod>
2121
<changefreq>daily</changefreq>
2222
</url>
2323
<url>
2424
<loc>None</loc>
25-
<lastmod>2022-01-24</lastmod>
25+
<lastmod>2022-01-26</lastmod>
2626
<changefreq>daily</changefreq>
2727
</url>
2828
<url>
2929
<loc>None</loc>
30-
<lastmod>2022-01-24</lastmod>
30+
<lastmod>2022-01-26</lastmod>
3131
<changefreq>daily</changefreq>
3232
</url>
3333
<url>
3434
<loc>None</loc>
35-
<lastmod>2022-01-24</lastmod>
35+
<lastmod>2022-01-26</lastmod>
3636
<changefreq>daily</changefreq>
3737
</url>
3838
<url>
3939
<loc>None</loc>
40-
<lastmod>2022-01-24</lastmod>
40+
<lastmod>2022-01-26</lastmod>
4141
<changefreq>daily</changefreq>
4242
</url>
4343
<url>
4444
<loc>None</loc>
45-
<lastmod>2022-01-24</lastmod>
45+
<lastmod>2022-01-26</lastmod>
4646
<changefreq>daily</changefreq>
4747
</url>
4848
<url>
4949
<loc>None</loc>
50-
<lastmod>2022-01-24</lastmod>
50+
<lastmod>2022-01-26</lastmod>
5151
<changefreq>daily</changefreq>
5252
</url>
5353
<url>
5454
<loc>None</loc>
55-
<lastmod>2022-01-24</lastmod>
55+
<lastmod>2022-01-26</lastmod>
5656
<changefreq>daily</changefreq>
5757
</url>
5858
<url>
5959
<loc>None</loc>
60-
<lastmod>2022-01-24</lastmod>
60+
<lastmod>2022-01-26</lastmod>
6161
<changefreq>daily</changefreq>
6262
</url>
6363
<url>
6464
<loc>None</loc>
65-
<lastmod>2022-01-24</lastmod>
65+
<lastmod>2022-01-26</lastmod>
6666
<changefreq>daily</changefreq>
6767
</url>
6868
<url>
6969
<loc>None</loc>
70-
<lastmod>2022-01-24</lastmod>
70+
<lastmod>2022-01-26</lastmod>
7171
<changefreq>daily</changefreq>
7272
</url>
7373
<url>
7474
<loc>None</loc>
75-
<lastmod>2022-01-24</lastmod>
75+
<lastmod>2022-01-26</lastmod>
7676
<changefreq>daily</changefreq>
7777
</url>
7878
<url>
7979
<loc>None</loc>
80-
<lastmod>2022-01-24</lastmod>
80+
<lastmod>2022-01-26</lastmod>
8181
<changefreq>daily</changefreq>
8282
</url>
8383
<url>
8484
<loc>None</loc>
85-
<lastmod>2022-01-24</lastmod>
85+
<lastmod>2022-01-26</lastmod>
8686
<changefreq>daily</changefreq>
8787
</url>
8888
<url>
8989
<loc>None</loc>
90-
<lastmod>2022-01-24</lastmod>
90+
<lastmod>2022-01-26</lastmod>
9191
<changefreq>daily</changefreq>
9292
</url>
9393
<url>
9494
<loc>None</loc>
95-
<lastmod>2022-01-24</lastmod>
95+
<lastmod>2022-01-26</lastmod>
9696
<changefreq>daily</changefreq>
9797
</url>
9898
<url>
9999
<loc>None</loc>
100-
<lastmod>2022-01-24</lastmod>
100+
<lastmod>2022-01-26</lastmod>
101101
<changefreq>daily</changefreq>
102102
</url>
103103
<url>
104104
<loc>None</loc>
105-
<lastmod>2022-01-24</lastmod>
105+
<lastmod>2022-01-26</lastmod>
106106
<changefreq>daily</changefreq>
107107
</url>
108108
<url>
109109
<loc>None</loc>
110-
<lastmod>2022-01-24</lastmod>
110+
<lastmod>2022-01-26</lastmod>
111111
<changefreq>daily</changefreq>
112112
</url>
113113
<url>
114114
<loc>None</loc>
115-
<lastmod>2022-01-24</lastmod>
115+
<lastmod>2022-01-26</lastmod>
116116
<changefreq>daily</changefreq>
117117
</url>
118118
<url>
119119
<loc>None</loc>
120-
<lastmod>2022-01-24</lastmod>
120+
<lastmod>2022-01-26</lastmod>
121121
<changefreq>daily</changefreq>
122122
</url>
123123
</urlset>

sitemap.xml.gz

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)