-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLecture_13
More file actions
55 lines (32 loc) · 996 Bytes
/
Lecture_13
File metadata and controls
55 lines (32 loc) · 996 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
@ strings
>>> eval('1 + 2 + 3')
6
>>> 'here' in 'Where's my dog?"
True
Now let's try to find the Fibonacci's even numbers.
def fib(n):
"""Return the nth Fibonacci number."""
if n == 1:
return 0
previous, current = 0, 1 # Current is the 2nd
for _ in range(2, n):
previous, current = current, previous + current
return current
def even(n):
return n % 2 == 0
def first_n_fibonaccis(n):
return sum(filter(even, map(fib, range(1, n+1))))
def acronym(name):
return tuple(map(first, filter(capitalized, name)))
def first(s):
return s[0]
def capitalized(s):
return len(s) > 0 and s[0].isupper()
berkeley = "University of California at Berkeley".split()
@ Reducing a Sequence
Reduce is a higher-order generalization of max, min, & sum.
>>> from operator import mul
>>> from functools import reduce
>>> reduce(mul, (1, 2, 3, 4, 5))
def acronym_generator(name):
return tuple(first(w) for w in name if capitalized(w))