Lecture Notes 03 May 2017 def cut_rod (p, 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 return r, s def main(): # define the price per length of rod p = [0, 1, 5, 8, 9, 10, 17, 17, 20] # prompt user for length of rod n = int (input ("Enter rod length: ")) # get the optimal price for cutting the rod of length n r, s = cut_rod (p, n) # print out the result for i in range (1, len(r)): print (i, r[i], s[i]) print () # print the optimal price print ("Optimal price = ", r[n]) # print the cuts of the rod while (n > 0): print (s[n]) n = n - s[n] main()