Skip to content

Commit ef37ce0

Browse files
committed
ch05
1 parent ee641c1 commit ef37ce0

Some content is hidden

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

46 files changed

+524
-0
lines changed

ch05/decorate.sort.undecorate.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# decorate.sort.undecorate.py
2+
from pprint import pprint
3+
4+
5+
students = [
6+
dict(id=0, credits=dict(math=9, physics=6, history=7)),
7+
dict(id=1, credits=dict(math=6, physics=7, latin=10)),
8+
dict(id=2, credits=dict(history=8, physics=9, chemistry=10)),
9+
dict(id=3, credits=dict(math=5, physics=5, geography=7)),
10+
]
11+
12+
13+
def decorate(student):
14+
# create a 2-tuple (sum of credits, student) from student dict
15+
return (sum(student['credits'].values()), student)
16+
pprint(decorate(students[0]))
17+
18+
19+
def undecorate(decorated_student):
20+
# discard sum of credits, return original student dict
21+
return decorated_student[1]
22+
23+
24+
students = sorted(map(decorate, students), reverse=True)
25+
students = list(map(undecorate, students))
26+
pprint(students)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# dictionary.comprehensions.duplicates.py
2+
word = 'Hello'
3+
4+
swaps = {c: c.swapcase() for c in word}
5+
6+
print(swaps) # prints: {'H': 'h', 'e': 'E', 'l': 'L', 'o': 'O'}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# dictionary.comprehensions.positions.py
2+
word = 'Hello'
3+
4+
positions = {c: k for k, c in enumerate(word)}
5+
6+
print(positions) # prints: {'H': 0, 'e': 1, 'l': 3, 'o': 4}

ch05/dictionary.comprehensions.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# dictionary.comprehensions.py
2+
from string import ascii_lowercase
3+
4+
5+
lettermap = {c: k for k, c in enumerate(ascii_lowercase, 1)}
6+
# lettermap = dict((c, k) for k, c in enumerate(ascii_lowercase, 1))
7+
8+
9+
from pprint import pprint
10+
pprint(lettermap)

ch05/even.squares.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# even.squares.py
2+
# using map and filter
3+
sq1 = list(
4+
map(lambda n: n ** 2, filter(lambda n: not n % 2, range(10)))
5+
)
6+
7+
# equivalent, but using list comprehensions
8+
sq2 = [n ** 2 for n in range(10) if not n % 2]
9+
10+
print(sq1, sq1 == sq2) # prints: [0, 4, 16, 36, 64] True

ch05/fibonacci.elegant.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# fibonacci.elegant.py
2+
def fibonacci(N):
3+
"""Return all fibonacci numbers up to N. """
4+
a, b = 0, 1
5+
while a <= N:
6+
yield a
7+
a, b = b, a + b
8+
9+
10+
print(list(fibonacci(0))) # [0]
11+
print(list(fibonacci(1))) # [0, 1, 1]
12+
print(list(fibonacci(50))) # [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

ch05/fibonacci.first.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# fibonacci.first.py
2+
def fibonacci(N):
3+
"""Return all fibonacci numbers up to N. """
4+
result = [0]
5+
next_n = 1
6+
while next_n <= N:
7+
result.append(next_n)
8+
next_n = sum(result[-2:])
9+
return result
10+
11+
12+
print(fibonacci(0)) # [0]
13+
print(fibonacci(1)) # [0, 1, 1]
14+
print(fibonacci(50)) # [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

ch05/fibonacci.second.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# fibonacci.second.py
2+
def fibonacci(N):
3+
"""Return all fibonacci numbers up to N. """
4+
yield 0
5+
if N == 0:
6+
return
7+
a = 0
8+
b = 1
9+
while b <= N:
10+
yield b
11+
a, b = b, a + b
12+
13+
14+
print(list(fibonacci(0))) # [0]
15+
print(list(fibonacci(1))) # [0, 1, 1]
16+
print(list(fibonacci(50))) # [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

ch05/filter.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# filter.py
2+
# This is not a valid Python module - Don't run it.
3+
4+
>>> test = [2, 5, 8, 0, 0, 1, 0]
5+
>>> list(filter(None, test))
6+
[2, 5, 8, 1]
7+
>>> list(filter(lambda x: x, test)) # equivalent to previous one
8+
[2, 5, 8, 1]
9+
>>> list(filter(lambda x: x > 4, test)) # keep only items > 4
10+
[5, 8]
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# first.n.squares.manual.method.py
2+
def get_squares_gen(n):
3+
for x in range(n):
4+
yield x ** 2
5+
6+
squares = get_squares_gen(3)
7+
print(squares.__next__()) # prints: 0
8+
print(squares.__next__()) # prints: 1
9+
print(squares.__next__()) # prints: 4
10+
# the following raises StopIteration, the generator is exhausted,
11+
# any further call to next will keep raising StopIteration
12+
print(squares.__next__())

0 commit comments

Comments
 (0)