Repository navigation
Expand file tree
/
Copy pathch06_ex1.py
More file actions
166 lines (136 loc) · 2.86 KB
/
Copy pathch06_ex1.py
File metadata and controls
166 lines (136 loc) · 2.86 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/env python3
"""Functional Python Programming
Chapter 6, Example Set 1
"""
def add(a,b):
"""Recursive addition.
>>> add( 3, 5 )
8
"""
if a == 0: return b
elif b == 0: return a
else: return add(a-1, b+1)
def fact(n):
"""Recursive Factorial
>>> fact(0)
1
>>> fact(1)
1
>>> fact(7)
5040
"""
if n == 0: return 1
else: return n*fact(n-1)
def facti(n):
"""Imperative Factorial
>>> fact(0)
1
>>> fact(1)
1
>>> fact(7)
5040
"""
if n == 0: return 1
f= 1
for i in range(2,n):
f= f*i
return f
def fastexp( a, n ):
"""Recursive exponentiation by squaring
>>> fastexp( 3, 11 )
177147
"""
if n == 0: return 1
elif n % 2 == 1: return a*fastexp(a,n-1)
else:
t= fastexp(a,n//2)
return t*t
def fib(n):
"""Fibonacci numbers with naive recursion
>>> fib(20)
6765
>>> fib(1)
1
"""
if n == 0: return 0
if n == 1: return 1
return fib(n-1) + fib(n-2)
def fibi(n):
"""Fibonacci numbers saving just two previous values
>>> fibi(20)
6765
>>> fibi(1)
1
>>> fibi(2)
1
>>> fibi(3)
2
"""
if n == 0: return 0
if n == 1: return 1
f_n2, f_n1 = 1, 1
for i in range(3, n+1):
f_n2, f_n1 = f_n1, f_n2+f_n1
return f_n1
def fibi2(n):
"""Fibonacci numbers with iteration and memoization
>>> fibi2(20)
6765
>>> fibi2(1)
1
"""
f = [0, 1] + [None for _ in range(2,n+1)]
for i in range(2,n+1):
f[i]= f[i-1]+f[i-2]
return f[n]
def mapr( f, collection ):
"""Recursive definition of map-like function.
>>> mapr( lambda x:2**x, [0, 1, 2, 3, 4] )
[1, 2, 4, 8, 16]
"""
if len(collection) == 0: return []
return mapr(f, collection[:-1]) + [ f(collection[-1]) ]
def mapf( f, C ):
"""Higher-Order definition of map
>>> list( mapf( lambda x:2**x, [0, 1, 2, 3, 4] ) )
[1, 2, 4, 8, 16]
"""
return (f(x) for x in C)
def mapg(f, C):
"""Generator definition of map
>>> list( mapg( lambda x:2**x, [0, 1, 2, 3, 4] ) )
[1, 2, 4, 8, 16]
"""
for x in C:
yield f(x)
def prodi( iterable ):
"""Imperative product
>>> prodi( [1,2,3,4,5,6,7] )
5040
"""
p= 1
for n in iterable:
p *= n
return p
def prodrc( collection ):
"""Recursive product with a collection
>>> prodrc( [1,2,3,4,5,6,7] )
5040
"""
if len(collection) == 0: return 1
return collection[0] * prodrc(collection[1:])
def prodri( iterable ):
"""Recursive product with an iterable
>>> prodri( iter([1,2,3,4,5,6,7]) )
5040
"""
try:
head= next(iterable)
except StopIteration:
return 1
return head*prodri(iterable)
def test():
import doctest
doctest.testmod(verbose=1)
if __name__ == "__main__":
test()