-
Notifications
You must be signed in to change notification settings - Fork 0
/
hw6.py
239 lines (219 loc) · 6.31 KB
/
hw6.py
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
# CS 350: Homework 6
# Due: Week of 5/16
# Name: Jacob Bentley
import math
from re import X
def machine(data, code):
i = 0
value = 0
for instruction in code:
if i >= len(data):
raise Exception("Ran out of numbers")
if instruction == "ADD":
value += data[i]
i += 1
elif instruction == "MUL":
value += data[i] * data[i+1]
i += 2
else:
raise Exception("Illegal Instruction: " + instruction)
if i < len(data):
raise Exception("has leftover numbers")
return value
###########################################################################
# Problem 1:
#
# I've constructed a new data processing language that I call addmul.
#
# It is a very simple language. Programs in addmul consist of two instructions:
#
# ADD takes a value from the data stream and adds it to the current total;
# MUL takes the next two numbers from the current data stream, multiplies them
# together, and adds them to the total.
#
# That's it.
#
# Your job is to take the data stream (just a list of numbers), and determine
# the program that will produce the largest value.
#
# Example:
#
# largestProgram([2,3,5]) should return
#
# ["ADD","MUL"]
#
# because this will return 17, where ["MUL","ADD"] will return 11, and
# ["ADD","ADD","ADD"] will return 10.
#
# You can run your program by calling machine([2,3,4], ["ADD","MUL"]).
#
# You can use `machine(numbers, largestProgram(numbers))` to test your
# algorithm on any list of numbers.
#
# Running time: O(2**n)
###########################################################################
def largestProgram(data):
"""
>>> largestProgram([2,3,5])
['ADD', 'MUL']
>>> largestProgram([6,3,5])
['MUL', 'ADD']
>>> largestProgram([3,3,3])
['MUL', 'ADD']
>>> largestProgram([9,4,7,6])
['MUL', 'MUL']
>>> largestProgram([9,4,7,6,5])
['MUL', 'MUL', 'ADD']
>>> largestProgram([1,4,7,6,5])
['ADD', 'MUL', 'MUL']
>>> largestProgram([1,4,0,6,5])
['ADD', 'ADD', 'ADD', 'MUL']
>>> largestProgram([1,4,0,6,-5])
['ADD', 'ADD', 'ADD', 'ADD', 'ADD']
"""
d = getLargest(data, len(data) - 1, {}, 0, [])
return d[max(d.keys())]
def getLargest(data, i, d, total, codes):
if i < 0:
d[total] = codes
return d
if i > 0:
d = getLargest(data[:i-1], i-2, d, \
total + data[i] * data[i-1], \
['MUL'] + codes)
return getLargest(data[:i], i-1, d, \
total + data[i], \
['ADD'] + codes)
###########################################################################
# Problem 2
#
# Implement the Floyd-Warshall algorithm from class.
#
# For example, the adjacency matrix
#
# [ [ 0, inf, -2, inf],
# [ 4, 0, 3, inf],
# [inf, inf, 0, 2],
# [inf, -1, inf, 0] ]
#
# should give the distance matrix
#
# [ [ 0, -1, -2, 0],
# [ 4, 0, 2, 4],
# [ 5, 1, 0, 2],
# [ 3, -1, 1, 0] ].
#
# Running Time: O(V**3)
###########################################################################
def floyd(g):
"""
>>> floyd([[0, math.inf, -2, math.inf], [4, 0, 3, math.inf], \
[math.inf, math.inf, 0, 2], [math.inf, -1, math.inf, 0]])
[[0, -1, -2, 0], [4, 0, 2, 4], [5, 1, 0, 2], [3, -1, 1, 0]]
"""
for i in range(len(g)):
for j in range(len(g)):
for k in range(len(g)):
g[j][k] = min(g[j][k], g[j][i] + g[i][k])
return g
###########################################################################
# Problem 3
#
# Congratulations! You now own a factory that cuts rods.
#
# Customers will pay a certain value for a length of rods. For example:
#
# rod length: 3 4 5 6 7
# price: 2 3 6 8 11
#
# You just received a rod of length d.
#
# Write a function to determine the most efficient way to cut the rod
# to maximize the profit.
#
# You should return the maximum profit you can make.
#
# Running Time: O(n)
###########################################################################
def rods(lengths, prices, d):
"""
>>> rods([3,4,5,6,7], [2,3,6,8,11], 20)
30
>>> rods([3,4,5,6,7], [2,3,6,8,11], 8)
11
>>> rods([3,4,5,6,7], [2,3,6,8,11], 10)
13
"""
revenue = 0
i = len(lengths) - 1
while d and i >= 0:
if d < lengths[i]:
i -= 1
else:
d -= lengths[i]
revenue += prices[i]
return revenue
############################################################################
# Problem 4: Parenthesizing matrices.
#
# This is the same problem as homework 4, problem 3, but this time I want
# you to do it in polynomial time using dynamic programming.
#
# If I multiply an m*l matrix A by an l*n matrix B, that will take O(n*l*m)
# time to compute.
#
# If I include a n*o matrix C in this product, then I have the m*o matrix
# A*B*C.
#
# This is perfectly well defined, but I have a choice:
#
# Do I multiply (A*B)*C, giving a running time of n*l*m + n*m*o,
# or do I multiply A*(B*C), giving a running time of l*m*o + n*l*o?
#
# Since matrix multiplication is associative, we will get the same answer.
#
# So, given a list of dimensions of matrices such as
#
# [(n,l), (l,m), (m,o)],
#
# compute the fastest running time in which we can do matrix multiplication.
#
# Example: [(3,5), (5,4), (4,7)] is 3*5*4 + 3*4*7 = 144.
#
# Running time: O(n)
############################################################################
def matrixParens(sizes):
"""
>>> matrixParens([(3,5), (5,4), (4,7)])
144
"""
if len(sizes) < 3:
return None
a, c = 0, 2
b, d = len(sizes) - 3, len(sizes) - 1
leftSum = rightSum = 0
# A and C go left to right, B and D go right to left
while c < len(sizes):
A, C = sizes[a], sizes[c]
B, D = sizes[b], sizes[d]
leftSum += A[0]*A[1]*C[0] + A[0]*C[0]*C[1]
rightSum += B[0]*B[1]*D[1] + B[1]*D[0]*D[1]
a, b, c, d = a+1, b-1, c+1, d-1
if leftSum < rightSum:
return leftSum
return rightSum
def eval(l, x):
"""
>>> eval([1, 2, 3], 3)
31
"""
result = i = 0
while i < len(l):
result += l[i] * x
i += 1
x -= 1
return result
# Enable doctesting:
if __name__ == "__main__":
import doctest
doctest.testmod()