From c315f3839c0e9635e742106b540aac8b01cea73c Mon Sep 17 00:00:00 2001 From: Ganesh Sanap <31900816+gsoc18@users.noreply.github.com> Date: Wed, 3 Oct 2018 23:13:09 +0530 Subject: [PATCH] Created cutRod.py Python Implementation of Rod Cutting. --- .../Dynamic Programming/cutRod/cutRod.py | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 Competitive Coding/Dynamic Programming/cutRod/cutRod.py diff --git a/Competitive Coding/Dynamic Programming/cutRod/cutRod.py b/Competitive Coding/Dynamic Programming/cutRod/cutRod.py new file mode 100644 index 000000000..a7cde8809 --- /dev/null +++ b/Competitive Coding/Dynamic Programming/cutRod/cutRod.py @@ -0,0 +1,23 @@ +# A Dynamic Programming solution for Rod cutting problem +INT_MIN = -32767 + +# Returns the best obtainable price for a rod of length n and +# price[] as prices of different pieces +def cutRod(price, n): + val = [0 for x in range(n+1)] + val[0] = 0 + + # Build the table val[] in bottom up manner and return + # the last entry from the table + for i in range(1, n+1): + max_val = INT_MIN + for j in range(i): + max_val = max(max_val, price[j] + val[i-j-1]) + val[i] = max_val + + return val[n] + +# Driver program to test above functions +arr = [1, 5, 8, 9, 10, 17, 17, 20] +size = len(arr) +print("Maximum Obtainable Value is " + str(cutRod(arr, size)))