Skip to content

Commit e0eb554

Browse files
author
Mia Steinkirch
committed
clean up
1 parent 1ef64d6 commit e0eb554

File tree

279 files changed

+24267
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

279 files changed

+24267
-0
lines changed

First_edition_2014/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
## Python & Algorithms Book 1st Edition (2014, a published by Hanbit Media)
2+
3+
➡️ 📚[Download PDF (first edition)](https://github.com/bt3gl/Python-and-Algorithms-and-Data-Structures/blob/master/First_edition_2014/ebook_pdf/book_second_edition.pdf).
4+
5+
6+
## Installation:
7+
8+
Install dependencies in a [virtual environment](https://coderwall.com/p/8-aeka):
9+
10+
```
11+
virtualenv venv
12+
source venv/bin/activate
13+
pip install -r requirements.txt
14+
```
4.42 MB
Binary file not shown.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env python3
2+
3+
__author__ = "bt3"
4+
5+
6+
from functools import lru_cache
7+
8+
9+
@lru_cache(maxsize=20)
10+
def fib(n):
11+
if n < 2:
12+
return n
13+
return fib(n-1) + fib(n-2)
14+
15+
16+
if __name__ == '__main__':
17+
print([fib(n) for n in range(10)])
18+
print(fib.cache_info())
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env python
2+
3+
__author__ = "bt3"
4+
5+
from collections import OrderedDict
6+
7+
def OrderedDict_example():
8+
''' show some examples for OrderedDict '''
9+
''' keep the order of insertion.
10+
maintains a doubly linked list, so size is more than twice than normal dict'''
11+
12+
13+
pairs = [('a', 1), ('b',2), ('c',3)]
14+
15+
d1 = {}
16+
for key, value in pairs:
17+
if key not in d1:
18+
d1[key] = []
19+
d1[key].append(value)
20+
for key in d1:
21+
print(key, d1[key])
22+
23+
d2 = OrderedDict(pairs)
24+
for key in d2:
25+
print(key, d2[key])
26+
27+
28+
if __name__ == '__main__':
29+
OrderedDict_example()
30+
31+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env python
2+
3+
__author__ = "bt3"
4+
5+
def simple2(a, *args):
6+
print args
7+
8+
def simple(*args):
9+
print args
10+
11+
def simple3(**kwargs):
12+
print kwargs
13+
14+
15+
simple(1, 2, 3)
16+
simple2(1, 2, 3)
17+
simple3(x=1, y=2)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env python
2+
3+
__author__ = "bt3"
4+
5+
6+
import random
7+
8+
def benchmark(func):
9+
import time
10+
def wrapper(*args, **kwargs):
11+
t = time.clock()
12+
res = func(*args, **kwargs)
13+
print("\t%s" % func.__name__, time.clock()-t)
14+
return res
15+
return wrapper
16+
17+
18+
@benchmark
19+
def random_tree(n):
20+
temp = [n for n in range(n)]
21+
for i in range(n+1):
22+
temp[random.choice(temp)] = random.choice(temp)
23+
return temp
24+
25+
26+
if __name__ == '__main__':
27+
random_tree(10000)
28+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env python
2+
3+
__author__ = "bt3"
4+
5+
a = [3, 4, 5, 6, 7]
6+
7+
8+
# Filter elements greater than 4
9+
10+
# Bad:
11+
12+
b = []
13+
for i in a:
14+
if i > 4:
15+
b.append(i)
16+
print b
17+
18+
# Good:
19+
print [i for i in a if i > 4]
20+
21+
# Or:
22+
print filter(lambda x: x > 4, a)
23+
24+
25+
# Add three to all list members:
26+
27+
# Bad
28+
b = []
29+
for i in range(len(a)):
30+
b.append(a[i] + 3)
31+
print b
32+
33+
# Good:
34+
print [i + 3 for i in a]
35+
36+
# Or:
37+
print map(lambda i: i + 3, a)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env python
2+
3+
__author__ = "bt3"
4+
5+
from collections import Counter
6+
7+
def Counter_example():
8+
''' it is a dictionary that maps the items to the number of occurrences '''
9+
seq1 = [1, 2, 3, 5, 1, 2, 5, 5, 2, 5, 1, 4]
10+
seq_counts = Counter(seq1)
11+
print(seq_counts)
12+
13+
''' we can increment manually or use the update() method '''
14+
seq2 = [1, 2, 3]
15+
seq_counts.update(seq2)
16+
print(seq_counts)
17+
18+
seq3 = [1, 4, 3]
19+
for key in seq3:
20+
seq_counts[key] += 1
21+
print(seq_counts)
22+
23+
''' also, we can use set operations such as a-b or a+b '''
24+
seq_counts_2 = Counter(seq3)
25+
print(seq_counts_2)
26+
print(seq_counts + seq_counts_2)
27+
print(seq_counts - seq_counts_2)
28+
29+
if __name__ == '__main__':
30+
Counter_example()
31+
32+
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/usr/bin/env python
2+
3+
__author__ = "bt3"
4+
5+
6+
7+
def logger(func):
8+
def inner(*args): #1
9+
print "Arguments were: {0}".format(args)
10+
return func(*args)
11+
return inner
12+
13+
@logger
14+
def foo(x, y):
15+
return x+y
16+
17+
print foo(1, 2)
18+
19+
20+
def sum(func):
21+
s = 0
22+
for i in func():
23+
s += i
24+
return s
25+
26+
@sum
27+
def interate():
28+
a = []
29+
for i in range(10):
30+
a.append(i)
31+
return a
32+
33+
print interate
34+
35+
# which is the same as
36+
def interate():
37+
a = []
38+
for i in range(10):
39+
a.append(i)
40+
return a
41+
42+
print sum(interate)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env python
2+
3+
__author__ = "bt3"
4+
5+
from collections import defaultdict
6+
7+
def defaultdict_example():
8+
''' show some examples for defaultdicts '''
9+
pairs = {('a', 1), ('b',2), ('c',3)}
10+
11+
d1 = {}
12+
for key, value in pairs:
13+
if key not in d1:
14+
d1[key] = []
15+
d1[key].append(value)
16+
print(d1)
17+
18+
d2 = defaultdict(list)
19+
for key, value in pairs:
20+
d2[key].append(value)
21+
print(d2)
22+
23+
24+
if __name__ == '__main__':
25+
defaultdict_example()
26+
27+

0 commit comments

Comments
 (0)