forked from kiat/Elements-of-Software-Design
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_010_rod_cutting.py
56 lines (39 loc) · 1.02 KB
/
example_010_rod_cutting.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
def cut_rod (p, n):
# Initialize two lists of zeros with size n
r = []
s = []
for i in range (n + 1):
r.append (0)
s.append (0)
for j in range (1, n + 1):
max_price = -1
for k in range (1, j + 1):
new_price = 0
if (k < len(p)):
new_price = p[k] + r[j - k]
else:
new_price = r[j - k]
if (new_price > max_price):
max_price = new_price
# remember the best value of the cut
s[j] = k
r[j] = max_price
print (r)
print (s)
return r, s
def main():
# define the price per length of rod
p = [0, 1, 5, 8, 9, 10, 17, 17, 20]
# prompt the user to enter the size of the rod to be cut
n = int(input ("Enter size of rod: "))
# get the optimal price for cutting a rod of length n
r, s = cut_rod(p, n)
# print the optimal price
print("Optimal price = ", r[n])
# print the cuts of the rod
while (n > 0):
print ("One Cut with size :", s[n])
n = n - s[n]
# Call the main function.
if __name__ == "__main__":
main()