From 051da83469fe3c707abf6d32de6423ba7b634138 Mon Sep 17 00:00:00 2001 From: Ishita <53152779+Ishita-0112@users.noreply.github.com> Date: Thu, 15 Oct 2020 20:06:02 +0530 Subject: [PATCH 1/3] Create RodCutting.py --- RodCutting.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 RodCutting.py diff --git a/RodCutting.py b/RodCutting.py new file mode 100644 index 0000000..a9bf84d --- /dev/null +++ b/RodCutting.py @@ -0,0 +1,24 @@ +# 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))) + From 6037aeb75ea0ebc13e2e4a22eaff98114d47fc92 Mon Sep 17 00:00:00 2001 From: Ishita <53152779+Ishita-0112@users.noreply.github.com> Date: Thu, 15 Oct 2020 20:09:48 +0530 Subject: [PATCH 2/3] Create CoinChange.py --- CoinChange.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 CoinChange.py diff --git a/CoinChange.py b/CoinChange.py new file mode 100644 index 0000000..6f7b9db --- /dev/null +++ b/CoinChange.py @@ -0,0 +1,29 @@ +# Dynamic Programming Python implementation of Coin +# Change problem +def count(S, m, n): + + # table[i] will be storing the number of solutions for + # value i. We need n+1 rows as the table is constructed + # in bottom up manner using the base case (n = 0) + # Initialize all table values as 0 + table = [0 for k in range(n+1)] + + # Base case (If given value is 0) + table[0] = 1 + + # Pick all coins one by one and update the table[] values + # after the index greater than or equal to the value of the + # picked coin + for i in range(0,m): + for j in range(S[i],n+1): + table[j] += table[j-S[i]] + + return table[n] + +# Driver program to test above function +arr = [1, 2, 3] +m = len(arr) +n = 4 +x = count(arr, m, n) +print (x) + From 24c3f19ba6c39d62c8ff4e52a5ccf6282040a896 Mon Sep 17 00:00:00 2001 From: Ishita <53152779+Ishita-0112@users.noreply.github.com> Date: Thu, 15 Oct 2020 20:12:21 +0530 Subject: [PATCH 3/3] Update CoinChange.py --- CoinChange.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CoinChange.py b/CoinChange.py index 6f7b9db..72e6a5f 100644 --- a/CoinChange.py +++ b/CoinChange.py @@ -1,4 +1,4 @@ -# Dynamic Programming Python implementation of Coin +# Dynamic Programming Coin Change Python implementation of Coin # Change problem def count(S, m, n):