-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithms_erickson.py
More file actions
284 lines (228 loc) · 7.62 KB
/
algorithms_erickson.py
File metadata and controls
284 lines (228 loc) · 7.62 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
### Recursion
# p. 4
def lattice(x: list[int], y: list[int]) -> list[int]:
n, m = len(x), len(y)
z = [None] * (n + m)
hold = 0
for k in range(n + m):
for i in range(min(n, k + 1)):
j = k - i
if j >= m:
continue
hold += x[i] * y[j]
z[k] = hold % 10
hold = hold // 10
return z
x, y = [5, 2], [5]
print(f"lattice({x}, {y}) = {lattice(x, y)}")
x, y = [2], [5]
print(f"lattice({x}, {y})) = {lattice(x, y)}")
x, y = [0, 0, 1], [1]
print(f"lattice({x}, {y}) = {lattice(x, y)}")
def peasant_iter(x: int, y: int) -> int:
prod = 0
while x > 0:
prod += y if x % 2 else 0
x, y = x // 2, 2 * y
return prod
print(peasant_iter(13, 2))
def peasant_rec(x: int, y: int) -> int:
if x == 0:
return 0
if x % 2 == 0:
return peasant_rec(x // 2, 2 * y)
else:
return y + peasant_rec(x // 2, 2 * y)
print(peasant_iter(1, 0))
print(peasant_iter(0, 1))
print(peasant_iter(0, 0))
print(peasant_iter(13, 2))
print(peasant_iter(23, 3))
# p. 42
def exp_iter(a, n):
res = a
for i in range(n -1):
res *= a
return res
print(exp_iter(2, 3))
def exp_rec(a, n):
if n == 1:
return a
res = exp_rec(a, n // 2) * exp_rec(a, n // 2)
return res if n % 2 == 0 else res * a
print(exp_rec(2, 5))
### Backtracking
#p. 77
def subset_sum(nums: list[int], target: int) -> bool:
"""Returns True if list of positive integers `nums` contains a subset of elements adding up to `target`"""
if target == 0:
return True
if target < 0 or len(nums) == 0: # actually 2nd condition: empty nums AND target != 0
# But that would be redundant with 1st base case
return False
x = nums[-1]
return (
subset_sum(nums[:-1], target - x)
or
subset_sum(nums[:-1], target)
)
print(f"Subset sum: {subset_sum([3, 1, 10, 2, 3], 7)}")
# Avoid copies of input array nums:
def subset_sum_idx(nums, i, target):
if target == 0:
return True
if target < 0 or i < 0:
return False
return (
subset_sum_idx(nums, i - 1, target - nums[i])
or
subset_sum_idx(nums, i - 1, target)
)
nums = [3, 1, 10, 2, 3]
print(f"Subset sum with indexes: {subset_sum_idx(nums, len(nums) - 1, 7)}")
# Can this algorithm be written iteratively?
# Recall this algorithm to build subsets
def build_subsets(nums):
subsets = [[]]
for num in nums:
subsets += [subset + [num] for subset in subsets]
return subsets
def subset_sum_iter(nums: list[int], target: int) -> bool:
"""Returns True if list of positive integers `nums` contains a subset of elements adding up to `target`"""
if target == 0:
return True
if target < 0 or len(nums) == 0:
return False
sums = [0]
for number in nums:
for curr_sum in tuple(sums):
new_sum = curr_sum + number
if new_sum == target:
return True
sums.append(new_sum)
return False
print("Subset sum iterative: ",
subset_sum_iter([3, 1, 10, 2, 3], 7),
subset_sum_iter([1, 2, 0, 2, 3], 5),
subset_sum_iter([1, 2, 0, 2, 3], 10),
subset_sum_iter([1, 2, 3], 6)
)
#p. 79
# Adapt the boolean function to actually build a subset;
def subset_sum_2(nums :list[int], i:int, target: int) -> list[int]|None:
if target == 0:
return []
if target < 0 or i < 0:
return
subset_without = subset_sum_2(nums, i - 1, target)
subset_with = subset_sum_2(nums, i - 1, target - nums[i])
return subset_with + [nums[i]] if subset_with is not None else subset_without
nums = [3, 1, 10, 2, 3]
print(f"ss2: {subset_sum_2(nums, len(nums) - 1, 7)}")
# Return all subsets with sum target
def subsets_sum(i, subset, target):
if target == 0:
subsets.append(subset[:])
return
if target < 0 or i < 0:
return
subsets_sum(i - 1, subset, target)
subset.append(nums[i])
subsets_sum(i - 1, subset, target - nums[i])
subset.pop()
return
nums = [3, 1, 10, 2, 3, 5]
subsets = []
subsets_sum(len(nums) - 1, [], 7)
print(f"All subsets: {subsets}")
#p. 80
def is_splittable(s: str) -> bool:
'''Returns True if string `s` can be split in words'''
if not s:
return True
for i in range(1, len(s) + 1):
if s[:i] in words and is_splittable(s[i:]):
return True
return False
words = set(["both", "earth", "and", "saturn", "spin", "bot", "heart", "hands", "at", "urns", "pin"])
print(f"""'bothearthandsaturnspin' is splittable: {is_splittable('bothearthandsaturnspin')}""")
# Indexed version
# Not useful in Python because slicing immutables creates copies
# https://stackoverflow.com/questions/64871329/does-string-slicing-perform-copy-in-memory
s = "bothearthandsaturnspin"
def is_splittable_idx(i: int) -> bool:
'''Returns True if string `s` can be split in words'''
if i == len(s):
return True
for j in range(i + 1, len(s) + 1):
if s[i:j] in words and is_splittable_idx(j):
return True
return False
print(f"""'bothearthandsaturnspin' is splittable (index): {is_splittable_idx(0)}""")
def splittable(i :int) -> bool:
return True if i == len(s) else any([s[i:j] in words and splittable(j) for j in range(i + 1, len(s) + 1)])
print(f"""'bothearthandsaturnspin' is splittable (condensed): {splittable(0)}""")
# p. 88
# Subset sum - like recursive pattern: taking/not taking element at index i
def lis(prev: int, curr: int) -> int:
if curr == len(nums):
return 0
skip = lis(prev, curr + 1)
prev_num = nums[prev] if prev >= 0 else float("-inf")
take = 1 + lis(curr, curr + 1) if prev_num < nums[curr] else skip
return max(skip, take)
nums = [7, 2, 4, 1, 3, 6, 5]
print(lis(prev=-1, curr=0))
# p. 90
# Alternative recursive pattern:
# Recurse on all potential next element of the subsequence
def lis2(curr: int) -> int:
curr_num = nums[curr] if curr >= 0 else float("-inf")
best_length = 0
for i in range(curr + 1, len(nums)):
if nums[i] > curr_num:
best_length = max(best_length, lis2(i))
return 1 + best_length if curr >= 0 else best_length # do not count -inf at index -1
nums = [7, 2, 4, 1, 3, 6, 5]
print(lis2(curr=-1))
### Dynamic Programming
# p. 105
s = "bothearthandsaturnspin"
cache = {}
def is_splittable_memoized(i: int) -> bool:
'''Returns True if string `s` can be split in words'''
if i == len(s):
return True
for j in range(i + 1, len(s) + 1):
if j not in cache:
cache[j] = is_splittable_memoized(j)
if s[i:j] in words and cache[j]:
return True
return False
print(f"""'bothearthandsaturnspin' is splittable (memoized): {is_splittable_memoized(0)}""")
# Notice that each subproblem is_splittable(i) depends only on is_splittable(j), j > i
# So fill the cache in reverse order:
def is_splittable_dp(s: str, words: set[str]):
n = len(s)
splittables = [False] * len(s) + [True]
for i in range(n - 1, -1, -1):
for j in range(i, n + 1):
if s[i:j] in words and splittables[j]:
splittables[i] = True
return splittables[0]
print(f"""'bothearthandsaturnspin' is splittable (DP): {is_splittable_dp(s, words)}""")
# p. 110
def lis_dp1(nums):
nums = [float("-inf")] + nums
n = len(nums)
array = [([None] * n) + [0] for i in range(n + 1)]
for j in range(n - 1, 0, -1):
for i in range(j):
if nums[i] >= nums[j]:
array[i][j] = array[i][j + 1]
else:
array[i][j] = max(array[i][j + 1], 1 + array[j][j + 1])
print(array)
return array[0][1]
nums = [7, 2, 4, 1, 3, 6, 5]
print(lis_dp1(nums))