Skip to content

Commit 605afdd

Browse files
committed
Deployed 45e9ec8 with MkDocs version: 1.0.4
1 parent 0f40726 commit 605afdd

File tree

6 files changed

+106
-30
lines changed

6 files changed

+106
-30
lines changed
Binary file not shown.

10_递归/recursion.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ def fact(n):
55
if n == 0:
66
return 1
77
else:
8-
return n * fact(n-1)
8+
return n * fact(n - 1)
99

1010

1111
def print_num(n):
@@ -15,14 +15,14 @@ def print_num(n):
1515

1616
def print_num_recursive(n):
1717
if n > 0:
18-
print_num_recursive(n-1)
18+
print_num_recursive(n - 1)
1919
print(n)
2020

2121

2222
def print_num_recursive_revserve(n):
2323
if n > 0:
2424
print(n)
25-
print_num_recursive_revserve(n-1)
25+
print_num_recursive_revserve(n - 1)
2626

2727

2828
from collections import deque
@@ -54,6 +54,19 @@ def print_num_use_stack(n):
5454

5555
def hanoi_move(n, source, dest, intermediate):
5656
if n >= 1: # 递归出口,只剩一个盘子
57-
hanoi_move(n-1, source, intermediate, dest)
57+
hanoi_move(n - 1, source, intermediate, dest)
5858
print("Move %s -> %s" % (source, dest))
59-
hanoi_move(n-1, intermediate, dest, source)
59+
hanoi_move(n - 1, intermediate, dest, source)
60+
61+
62+
def flatten(rec_list):
63+
for i in rec_list:
64+
if isinstance(i, list):
65+
for i in flatten(i):
66+
yield i
67+
else:
68+
yield i
69+
70+
71+
def test_flatten():
72+
assert list(flatten([[[1], 2, 3], [1, 2, 3]])) == [1, 2, 3, 1, 2, 3]

15_堆与堆排序/lfu.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,66 @@
44
55
这里学习下 LRU(least frequently used),就是当缓存满了之后剔除一个最少使用的 key。
66
"""
7+
from collections import defaultdict, OrderedDict
8+
9+
10+
class Node:
11+
__slots__ = 'key', 'val', 'cnt'
12+
13+
def __init__(self, key, val, cnt=0):
14+
self.key, self.val, self.cnt = key, val, cnt
15+
16+
17+
class LFUCache:
18+
def __init__(self, capacity):
19+
self.capacity = capacity
20+
self.cache = {} # type {key: node}
21+
self.cnt2node = defaultdict(OrderedDict)
22+
self.mincnt = 0
23+
24+
def get(self, key, default=-1):
25+
if key not in self.cache:
26+
return default
27+
28+
node = self.cache[key]
29+
del self.cnt2node[node.cnt][key]
30+
31+
if not self.cnt2node[node.cnt]:
32+
del self.cnt2node[node.cnt]
33+
34+
node.cnt += 1
35+
self.cnt2node[node.cnt][key] = node
36+
37+
if not self.cnt2node[self.mincnt]:
38+
self.mincnt += 1
39+
return node.val
40+
41+
def put(self, key, value):
42+
if key in self.cache:
43+
self.cache[key].val = value
44+
self.get(key)
45+
return
46+
if len(self.cache) >= self.capacity:
47+
pop_key, _pop_node = self.cnt2node[self.mincnt].popitem(last=False)
48+
del self.cache[pop_key]
49+
50+
self.cache[key] = self.cnt2node[1][key] = Node(key, value, 1)
51+
self.mincnt = 1
52+
53+
54+
def test():
55+
c = LFUCache(2)
56+
c.put(1, 1)
57+
c.put(2, 2)
58+
assert c.get(1) == 1
59+
c.put(3, 3)
60+
assert c.get(2) == -1
61+
assert c.get(3) == 3
62+
c.put(4, 4)
63+
assert c.get(1) == -1
64+
assert c.get(3) == 3
65+
assert c.get(4) == 4
66+
67+
68+
if __name__ == '__main__':
69+
test()

index.html

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

582582
<!--
583583
MkDocs version : 1.0.4
584-
Build Date UTC : 2018-12-24 10:49:23
584+
Build Date UTC : 2018-12-26 07:57:35
585585
-->

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

sitemap.xml.gz

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)