Skip to content

Commit d20ee18

Browse files
committed
added cutting rod problem in python
1 parent 9fc54b3 commit d20ee18

File tree

4 files changed

+182
-2
lines changed

4 files changed

+182
-2
lines changed

a.out

-13.8 KB
Binary file not shown.

cutting_rod.py

+175
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
'''
2+
Dynamic Programming:
3+
4+
not adding c++ files for now. Will add soon
5+
6+
Given a rod of length n inches and an array of prices that contains prices of all pieces of size smaller than n. Determine the maximum value obtainable by cutting up the rod and selling the pieces.
7+
8+
Input Format:
9+
---------------
10+
1 5 8 9 10 17 17 20
11+
12+
13+
1 2 3 4 5 6 7 8
14+
15+
total no of enteries are the length of the rod
16+
17+
Algo strategy:
18+
-Create the possible permutations in which the rod can be cut up and add up the max vals
19+
20+
How:
21+
val(l) = max{{ val(l-r)+val(r) } for r in range(0, l)}
22+
23+
# output:
24+
25+
non memoized:
26+
1 1
27+
1 5
28+
1 8
29+
1 1
30+
1 1
31+
5 5
32+
1 10
33+
1 1
34+
1 1
35+
1 5
36+
5 8
37+
1 13
38+
1 1
39+
1 1
40+
1 5
41+
1 8
42+
1 1
43+
1 1
44+
5 5
45+
5 10
46+
1 1
47+
1 5
48+
1 1
49+
1 5
50+
8 8
51+
1 17
52+
1 1
53+
1 1
54+
1 5
55+
1 8
56+
1 1
57+
1 1
58+
5 5
59+
1 10
60+
1 1
61+
1 1
62+
1 5
63+
5 8
64+
5 13
65+
1 1
66+
1 5
67+
1 1
68+
1 5
69+
1 8
70+
1 1
71+
1 1
72+
5 5
73+
8 10
74+
1 18
75+
1 1
76+
1 1
77+
1 5
78+
1 8
79+
1 1
80+
1 1
81+
5 5
82+
1 10
83+
1 1
84+
1 1
85+
1 5
86+
5 8
87+
1 13
88+
1 1
89+
1 1
90+
1 5
91+
1 8
92+
1 1
93+
1 1
94+
5 5
95+
5 10
96+
1 1
97+
1 5
98+
1 1
99+
1 5
100+
8 8
101+
5 17
102+
1 1
103+
1 5
104+
1 1
105+
1 5
106+
1 8
107+
1 1
108+
1 1
109+
5 5
110+
1 10
111+
1 1
112+
1 1
113+
1 5
114+
5 8
115+
8 13
116+
1 1
117+
1 5
118+
1 8
119+
1 1
120+
1 1
121+
5 5
122+
1 1
123+
1 5
124+
1 8
125+
1 1
126+
1 1
127+
5 5
128+
10 10
129+
22
130+
131+
132+
memoized:
133+
lesser no of calls
134+
135+
1 1
136+
1 5
137+
1 8
138+
5 5
139+
1 10
140+
5 8
141+
1 13
142+
5 10
143+
8 8
144+
1 17
145+
5 13
146+
8 10
147+
1 18
148+
5 17
149+
8 13
150+
10 10
151+
22
152+
153+
'''
154+
155+
# haha memoized code for rod cutting
156+
157+
s = list(map(int, input().split(" ")))
158+
m = {}
159+
def solve(l):
160+
if l in m:
161+
return m[l]
162+
val = []
163+
val.append(s[l-1])
164+
if(l == 1):
165+
m[1] = s[0]
166+
return m[1]
167+
for i in range(1, int(l/2)+1):
168+
a, b = solve(i), solve(l-i)
169+
print(a," ", b)
170+
val.append(( a+b ))
171+
m[l] = max(val)
172+
return m[l]
173+
174+
print(solve(8))
175+

ip.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
12 11 13 5 6 7
1+
1 5 8 9 10 17 17 20

readme.txt

+6-1
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,9 @@ Look out for important notes and flashcard points in that folder
3333

3434
---------------------------------------------------------------------------
3535

36-
The folder OOP contains some important OOP programming concepts that I have practiced
36+
The folder OOP contains some important OOP programming concepts that I have practiced
37+
--------------------------------------------------------------------------
38+
39+
Pending:
40+
41+
Cutting_Rod question in c++

0 commit comments

Comments
 (0)