-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rod_cutting.py
55 lines (36 loc) · 1.14 KB
/
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
""" This program implements a dynamic problem of Rod-Cutting
We will be using a Bottom-up-Approach to solve the problem
"""
def Bottom_up_rod_cut(p,n):
revenue_array = []
revenue_array.append(0)
#for i in range(n):
# revenue_array.append(0)
#print revenue_array
j = 0
while(j!=n):
local_sum = -9999
i = 0
while(i<=j):
local_sum = max(local_sum,p[i]+revenue_array[j-i])
i=i+1
revenue_array.append(local_sum)
j=j+1
return revenue_array[n]
try:
size = int(raw_input("\nEnter the integral length of rod\n"))
if(size == 0):
print "\n No rod to cut\n"
exit(1)
Pi = []
#Pi.append(0)
print "\nEnter the cost of length of rods\n"
for i in range(size):
print "\ncost of",i+1,"unit rod"
Pi.append(int(raw_input()))
print Pi
maximum_revenue = Bottom_up_rod_cut(Pi,size)
print "\n Maximum revenue =",maximum_revenue,"\n"
except ValueError:
print "\nError: Enter integer values only\n"
exit(0)