Skip to content

Commit 43a4971

Browse files
committed
Deployed 95d596e with MkDocs version: 1.0.4
1 parent 70a333e commit 43a4971

File tree

10 files changed

+107
-55
lines changed

10 files changed

+107
-55
lines changed

03_链表/double_link_list.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33

44
class Node(object):
5+
__slots__ = ('value', 'prev', 'next') # save memory
56

67
def __init__(self, value=None, prev=None, next=None):
78
self.value, self.prev, self.next = value, prev, next

03_链表/linked_list/index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,9 @@ <h1 id="_3">双链表</h1>
293293
因为缓存在 dict 里查找的时间是 O(1),你更新访问顺序就 O(n)了,缓存就没了优势。</p>
294294
<p>这里就要使用到双链表了,相比单链表来说,每个节点既保存了指向下一个节点的指针,同时还保存了上一个节点的指针。</p>
295295
<pre><code class="py">class Node(object):
296+
# 如果节点很多,我们可以用 __slots__ 来节省内存,把属性保存在一个 tuple 而不是 dict 里
297+
# 感兴趣可以自行搜索 python __slots__
298+
__slots__ = ('value', 'prev', 'next')
296299

297300
def __init__(self, value=None, prev=None, next=None):
298301
self.value, self.prev, self.next = value, prev, next

03_链表/lru_cache.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77

88

99
def fib(n):
10-
if n <= 1:
11-
return 1
12-
return f(n - 1) + f(n - 2) # 由于涉及到重复计算,这个递归函数在 n 大了以后会非常慢
10+
if n <= 1: # 0 or 1
11+
return n
12+
return f(n - 1) + f(n - 2) # 由于涉及到重复计算,这个递归函数在 n 大了以后会非常慢。 O(2^n)
1313

1414

1515
"""
@@ -34,8 +34,8 @@ def _(n): # 这里函数没啥意义就随便用下划线命名了
3434

3535
@cache
3636
def f(n):
37-
if n <= 1:
38-
return 1
37+
if n <= 1: # 0 or 1
38+
return n
3939
return f(n - 1) + f(n - 2)
4040

4141

@@ -108,8 +108,8 @@ def _(n):
108108

109109
@LRUCache(10)
110110
def f_use_lru(n):
111-
if n <= 1:
112-
return 1
111+
if n <= 1: # 0 or 1
112+
return n
113113
return f(n - 1) + f(n - 2)
114114

115115

13_高级排序算法/merge_sort.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,11 @@ def merge_sorted_list(sorted_a, sorted_b):
3232
new_sorted_seq.append(sorted_b[b])
3333
b += 1
3434

35-
while a < length_a:
36-
new_sorted_seq.append(sorted_a[a])
37-
a += 1
38-
39-
while b < length_b:
40-
new_sorted_seq.append(sorted_b[b])
41-
b += 1
35+
# 如果 a或b 中还有剩余元素,需要放到最后
36+
if a < length_a:
37+
new_sorted_seq.extend(sorted_a[a:])
38+
else:
39+
new_sorted_seq.extend(sorted_b[b:])
4240

4341
return new_sorted_seq
4442

13_高级排序算法/merge_sort/index.html

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -284,13 +284,10 @@ <h1 id="_1">归并排序</h1>
284284
b += 1
285285

286286
# 最后别忘记把多余的都放到有序数组里
287-
while a &lt; length_a:
288-
new_sorted_seq.append(sorted_a[a])
289-
a += 1
290-
291-
while b &lt; length_b:
292-
new_sorted_seq.append(sorted_b[b])
293-
b += 1
287+
if a &lt; length_a:
288+
new_sorted_seq.extend(sorted_a[a:])
289+
else:
290+
new_sorted_seq.extend(sorted_b[b:])
294291

295292
return new_sorted_seq
296293
</code></pre>

17_二叉查找树/binary_search_tree/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -445,8 +445,8 @@ <h1 id="_8">练习题:</h1>
445445
<h1 id="_9">延伸阅读</h1>
446446
<ul>
447447
<li>《Data Structures and Algorithms in Python》14 章,树的概念和算法还有很多,我们这里介绍最基本的帮你打个基础</li>
448-
<li>了解 mysql 索引使用的 B-Tree 结构,这个是后端面试数据库的常考点。想想为什么?当元素非常多的时候,二叉树的深度会很深,导致多次磁盘查找。<a href="https://blog.csdn.net/v_JULY_v/article/details/6530142">从B树、B+树、B*树谈到R
449-
</a></li>
448+
<li>了解红黑树。普通二叉查找树有个很大的问题就是难以保证树的平衡,极端情况下某些节点可能会非常深,导致查找复杂度大幅退化。而平衡二叉树就是为了解决这个问题。请搜索对应资料了解下。</li>
449+
<li>了解 mysql 索引使用的 B-Tree 结构(多路平衡查找树),这个是后端面试数据库的常考点。想想为什么?当元素非常多的时候,二叉树的深度会很深,导致多次磁盘查找。<a href="https://blog.csdn.net/v_JULY_v/article/details/6530142">从B树、B+树、B*树谈到R</a></li>
450450
</ul>
451451

452452
</div>

index.html

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,15 @@
9696

9797
<li><a class="toctree-l3" href="#_17">开发和测试工具</a></li>
9898

99-
<li><a class="toctree-l3" href="#_18">勘误</a></li>
99+
<li><a class="toctree-l3" href="#_18">测试用例设计</a></li>
100+
101+
<li><a class="toctree-l3" href="#_19">勘误</a></li>
100102

101103
<li><a class="toctree-l3" href="#git">如何更新代码(写给不熟悉 git 的同学)</a></li>
102104

103-
<li><a class="toctree-l3" href="#_19">如何提问?</a></li>
105+
<li><a class="toctree-l3" href="#_20">如何提问?</a></li>
104106

105-
<li><a class="toctree-l3" href="#_20">本电子书制作和写作方式</a></li>
107+
<li><a class="toctree-l3" href="#_21">本电子书制作和写作方式</a></li>
106108

107109
</ul>
108110

@@ -430,7 +432,58 @@ <h2 id="_17">开发和测试工具</h2>
430432

431433
<p>每次我们改动了代码,就会自动执行代码里的单元测试了。pytest 会自动发现以 test
432434
开头的函数并执行测试代码。良好的工程需要我们用单测来保证,将来即使修改了内部实现逻辑也方便做回归验证。</p>
433-
<h2 id="_18">勘误</h2>
435+
<p>或者你可以在的 ~/.bashrc or ~/.zshrc 里边加上这个映射(别忘记加上之后source下):</p>
436+
<pre><code class="sh"># 监控当前文件夹文件变动自动执行命令
437+
alias watchtest='when-changed -v -r -1 -s ./ '
438+
</code></pre>
439+
440+
<p>然后在你的代码目录里头执行 <code>watchtest pytest -s somefile.py</code> 一样的效果</p>
441+
<h2 id="_18">测试用例设计</h2>
442+
<p>笔者在刚学习编程的时候总是忘记处理一些特例(尤其是动态语言可以传各种值),为了养成良好的编程和测试习惯,在编写单元测试用例的时候,
443+
我们注意考虑下如下测试用例(等价类划分):</p>
444+
<ul>
445+
<li>正常值功能测试</li>
446+
<li>边界值(比如最大最小,最左最右值)</li>
447+
<li>异常值(比如 None,空值,非法值)</li>
448+
</ul>
449+
<pre><code>def binary_search(array, target):
450+
if not array:
451+
return -1
452+
beg, end = 0, len(array)
453+
while beg &lt; end:
454+
mid = beg + (end - beg) // 2 # py3
455+
if array[mid] == target:
456+
return mid
457+
elif array[mid] &gt; target:
458+
end = mid
459+
else:
460+
beg = mid + 1
461+
return -1
462+
463+
464+
def test():
465+
&quot;&quot;&quot;
466+
如何设计测试用例:
467+
- 正常值功能测试
468+
- 边界值(比如最大最小,最左最右值)
469+
- 异常值(比如 None,空值,非法值)
470+
&quot;&quot;&quot;
471+
# 正常值,包含有和无两种结果
472+
assert binary_search([0, 1, 2, 3, 4, 5], 1) == 1
473+
assert binary_search([0, 1, 2, 3, 4, 5], 6) == -1
474+
assert binary_search([0, 1, 2, 3, 4, 5], -1) == -1
475+
# 边界值
476+
assert binary_search([0, 1, 2, 3, 4, 5], 0) == 0
477+
assert binary_search([0, 1, 2, 3, 4, 5], 5) == 5
478+
assert binary_search([0], 0) == 0
479+
480+
# 异常值
481+
assert binary_search([], 1) == -1
482+
</code></pre>
483+
484+
<p>当然我们也不用做的非常细致,要不然写测试是一件非常繁琐累人的事情,甚至有时候为了测试而测试,只是为了让单测覆盖率好看点。
485+
当然如果是web应用用户输入,我们要假设所有的参数都是不可信的。 但是很多内部调用的函数我们基于约定来编程,如果你瞎传参数,那就是调用者的责任了。</p>
486+
<h2 id="_19">勘误</h2>
434487
<p>输出其实也是一种再学习的过程,中途需要查看大量资料、编写讲义、视频录制、代码编写等,难免有疏漏甚至错误之处。
435488
有出版社找过笔者想让我出书,一来自己对出书兴趣不大,另外感觉书籍相对视频不够直观,有错误也不能及时修改,打算直接把所有文字内容讲义和代码等放到 github 上,供大家免费查阅。</p>
436489
<p>如果你发现文字内容、代码内容、视频内容有错误或者有疑问,欢迎在 github 上提 issue 讨论(或者网易公开课评论区),或者直接提 Merge Request,我会尽量及时修正相关内容,防止对读者产生误导。
@@ -449,15 +502,15 @@ <h2 id="git">如何更新代码(写给不熟悉 git 的同学)</h2>
449502
</code></pre>
450503

451504
<p>然后使用 <code>git pull pegasuswang master</code> 拉取更新。</p>
452-
<h2 id="_19">如何提问?</h2>
505+
<h2 id="_20">如何提问?</h2>
453506
<p>如果读者关于代码、视频、讲义有任何疑问,欢迎一起讨论
454507
请注意以下几点:</p>
455508
<ul>
456509
<li>优先在网易云课堂的讨论区提问,方便别的同学浏览。如果未购买视频,也可以直接在 github 里提出 issue,笔者有空会给大家解答和讨论。</li>
457510
<li>描述尽量具体,视频或者代码哪一部分有问题?请尽量把涉及章节和代码贴出来,方便定位问题。</li>
458511
<li>如果涉及到代码,提问时请保持代码的格式</li>
459512
</ul>
460-
<h2 id="_20">本电子书制作和写作方式</h2>
513+
<h2 id="_21">本电子书制作和写作方式</h2>
461514
<p>使用 mkdocs 和 markdown 构建,使用 Python-Markdown-Math 完成数学公式。
462515
markdown 语法参考:http://xianbai.me/learn-md/article/about/readme.html</p>
463516
<p>安装依赖:</p>
@@ -528,5 +581,5 @@ <h2 id="_20">本电子书制作和写作方式</h2>
528581

529582
<!--
530583
MkDocs version : 1.0.4
531-
Build Date UTC : 2018-12-22 02:52:07
584+
Build Date UTC : 2018-12-22 16:07:34
532585
-->

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>2018-12-22</lastmod>
5+
<lastmod>2018-12-23</lastmod>
66
<changefreq>daily</changefreq>
77
</url>
88
<url>
99
<loc>None</loc>
10-
<lastmod>2018-12-22</lastmod>
10+
<lastmod>2018-12-23</lastmod>
1111
<changefreq>daily</changefreq>
1212
</url>
1313
<url>
1414
<loc>None</loc>
15-
<lastmod>2018-12-22</lastmod>
15+
<lastmod>2018-12-23</lastmod>
1616
<changefreq>daily</changefreq>
1717
</url>
1818
<url>
1919
<loc>None</loc>
20-
<lastmod>2018-12-22</lastmod>
20+
<lastmod>2018-12-23</lastmod>
2121
<changefreq>daily</changefreq>
2222
</url>
2323
<url>
2424
<loc>None</loc>
25-
<lastmod>2018-12-22</lastmod>
25+
<lastmod>2018-12-23</lastmod>
2626
<changefreq>daily</changefreq>
2727
</url>
2828
<url>
2929
<loc>None</loc>
30-
<lastmod>2018-12-22</lastmod>
30+
<lastmod>2018-12-23</lastmod>
3131
<changefreq>daily</changefreq>
3232
</url>
3333
<url>
3434
<loc>None</loc>
35-
<lastmod>2018-12-22</lastmod>
35+
<lastmod>2018-12-23</lastmod>
3636
<changefreq>daily</changefreq>
3737
</url>
3838
<url>
3939
<loc>None</loc>
40-
<lastmod>2018-12-22</lastmod>
40+
<lastmod>2018-12-23</lastmod>
4141
<changefreq>daily</changefreq>
4242
</url>
4343
<url>
4444
<loc>None</loc>
45-
<lastmod>2018-12-22</lastmod>
45+
<lastmod>2018-12-23</lastmod>
4646
<changefreq>daily</changefreq>
4747
</url>
4848
<url>
4949
<loc>None</loc>
50-
<lastmod>2018-12-22</lastmod>
50+
<lastmod>2018-12-23</lastmod>
5151
<changefreq>daily</changefreq>
5252
</url>
5353
<url>
5454
<loc>None</loc>
55-
<lastmod>2018-12-22</lastmod>
55+
<lastmod>2018-12-23</lastmod>
5656
<changefreq>daily</changefreq>
5757
</url>
5858
<url>
5959
<loc>None</loc>
60-
<lastmod>2018-12-22</lastmod>
60+
<lastmod>2018-12-23</lastmod>
6161
<changefreq>daily</changefreq>
6262
</url>
6363
<url>
6464
<loc>None</loc>
65-
<lastmod>2018-12-22</lastmod>
65+
<lastmod>2018-12-23</lastmod>
6666
<changefreq>daily</changefreq>
6767
</url>
6868
<url>
6969
<loc>None</loc>
70-
<lastmod>2018-12-22</lastmod>
70+
<lastmod>2018-12-23</lastmod>
7171
<changefreq>daily</changefreq>
7272
</url>
7373
<url>
7474
<loc>None</loc>
75-
<lastmod>2018-12-22</lastmod>
75+
<lastmod>2018-12-23</lastmod>
7676
<changefreq>daily</changefreq>
7777
</url>
7878
<url>
7979
<loc>None</loc>
80-
<lastmod>2018-12-22</lastmod>
80+
<lastmod>2018-12-23</lastmod>
8181
<changefreq>daily</changefreq>
8282
</url>
8383
<url>
8484
<loc>None</loc>
85-
<lastmod>2018-12-22</lastmod>
85+
<lastmod>2018-12-23</lastmod>
8686
<changefreq>daily</changefreq>
8787
</url>
8888
<url>
8989
<loc>None</loc>
90-
<lastmod>2018-12-22</lastmod>
90+
<lastmod>2018-12-23</lastmod>
9191
<changefreq>daily</changefreq>
9292
</url>
9393
<url>
9494
<loc>None</loc>
95-
<lastmod>2018-12-22</lastmod>
95+
<lastmod>2018-12-23</lastmod>
9696
<changefreq>daily</changefreq>
9797
</url>
9898
<url>
9999
<loc>None</loc>
100-
<lastmod>2018-12-22</lastmod>
100+
<lastmod>2018-12-23</lastmod>
101101
<changefreq>daily</changefreq>
102102
</url>
103103
<url>
104104
<loc>None</loc>
105-
<lastmod>2018-12-22</lastmod>
105+
<lastmod>2018-12-23</lastmod>
106106
<changefreq>daily</changefreq>
107107
</url>
108108
<url>
109109
<loc>None</loc>
110-
<lastmod>2018-12-22</lastmod>
110+
<lastmod>2018-12-23</lastmod>
111111
<changefreq>daily</changefreq>
112112
</url>
113113
<url>
114114
<loc>None</loc>
115-
<lastmod>2018-12-22</lastmod>
115+
<lastmod>2018-12-23</lastmod>
116116
<changefreq>daily</changefreq>
117117
</url>
118118
<url>
119119
<loc>None</loc>
120-
<lastmod>2018-12-22</lastmod>
120+
<lastmod>2018-12-23</lastmod>
121121
<changefreq>daily</changefreq>
122122
</url>
123123
</urlset>

sitemap.xml.gz

1 Byte
Binary file not shown.

0 commit comments

Comments
 (0)