From c7c87a1e842aec4a9a5d1c9b622e44937f33fc8a Mon Sep 17 00:00:00 2001 From: JatinR05 <71865805+JatinR05@users.noreply.github.com> Date: Mon, 24 Oct 2022 11:35:49 +0530 Subject: [PATCH 1/6] Create recursive_approach_knapsack.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Added a new naïve recursive approach to solve the knapsack problem. --- knapsack/recursive_approach_knapsack.py | 67 +++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 knapsack/recursive_approach_knapsack.py diff --git a/knapsack/recursive_approach_knapsack.py b/knapsack/recursive_approach_knapsack.py new file mode 100644 index 000000000000..49515c5e19c4 --- /dev/null +++ b/knapsack/recursive_approach_knapsack.py @@ -0,0 +1,67 @@ +# To get an insight into naive recursive way to solve the Knapsack problem + + +""" +A shopkeeper has bags of wheat that each have different weights and different profits. +eg. +no_of_items 4 +profit 5 4 8 6 +weight 1 2 4 5 +max_weight 5 +Constraints: +max_weight > 0 +profit[i] >= 0 +weight[i] >= 0 +Calculate the maximum profit that the shopkeeper can make given maxmum weight that can +be carried. +""" + +def knapsack(weights: list, values: list, n: int, max_weight: int,index: int) -> int : + """ + Function description is as follows- + :param weights: Take a list of weights + :param values: Take a list of profits corresponding to the weights + :param max_weight: Maximum weight that could be carried + :param index: the element we are looking at + :return: Maximum expected gain + >>> knapsack([1, 2, 4, 5], [5, 4, 8, 6], 4, 15) + 13 + >>> knapsack([3 ,4 , 5], [10, 9 , 8], 3, 25) + 27 + """ + if index == len(weights): + return 0 + ans1 = 0 + ans2 = 0 + ans1 = knapsack(weights,values,n,max_weight,index+1) + if weights[index] <= max_weight: + ans2 = values[index] + knapsack(weights,values,n,max_weight-weights[index],index+1) + return max(ans1,ans2) + +def take_input() : + """ + This function is to take input from the user + """ + n = int(input("Input number of items: ")) + + if n == 0 : + return list(), list(), n, 0 + + weights = list(map(int, input("Input weights separated by spaces: ").split(" "))) + values = list(map(int, input("Input profits separated by spaces: ").split(" "))) + max_weight = int(input("Max weight allowed: ")) + + return weights, values, n, max_weight + + + + +if __name__ == "__main__": + print( + "Input profits, weights, and then max_weight (all positive ints) separated by " + "spaces." + ) + weights, values, n, max_weight = take_input() + + # Function Call + print(knapsack(weights, values, n, max_weight,0)) From 1702cb4e1da33351837963300a3965b096aa5aeb Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 24 Oct 2022 06:08:31 +0000 Subject: [PATCH 2/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- knapsack/recursive_approach_knapsack.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/knapsack/recursive_approach_knapsack.py b/knapsack/recursive_approach_knapsack.py index 49515c5e19c4..f769e0ba6ae7 100644 --- a/knapsack/recursive_approach_knapsack.py +++ b/knapsack/recursive_approach_knapsack.py @@ -16,7 +16,8 @@ be carried. """ -def knapsack(weights: list, values: list, n: int, max_weight: int,index: int) -> int : + +def knapsack(weights: list, values: list, n: int, max_weight: int, index: int) -> int: """ Function description is as follows- :param weights: Take a list of weights @@ -33,18 +34,21 @@ def knapsack(weights: list, values: list, n: int, max_weight: int,index: int) -> return 0 ans1 = 0 ans2 = 0 - ans1 = knapsack(weights,values,n,max_weight,index+1) + ans1 = knapsack(weights, values, n, max_weight, index + 1) if weights[index] <= max_weight: - ans2 = values[index] + knapsack(weights,values,n,max_weight-weights[index],index+1) - return max(ans1,ans2) + ans2 = values[index] + knapsack( + weights, values, n, max_weight - weights[index], index + 1 + ) + return max(ans1, ans2) + -def take_input() : +def take_input(): """ This function is to take input from the user """ n = int(input("Input number of items: ")) - if n == 0 : + if n == 0: return list(), list(), n, 0 weights = list(map(int, input("Input weights separated by spaces: ").split(" "))) @@ -54,8 +58,6 @@ def take_input() : return weights, values, n, max_weight - - if __name__ == "__main__": print( "Input profits, weights, and then max_weight (all positive ints) separated by " @@ -64,4 +66,4 @@ def take_input() : weights, values, n, max_weight = take_input() # Function Call - print(knapsack(weights, values, n, max_weight,0)) + print(knapsack(weights, values, n, max_weight, 0)) From a4de92e96f1f09657cb6c8dd05411b029efb97b2 Mon Sep 17 00:00:00 2001 From: JatinR05 <71865805+JatinR05@users.noreply.github.com> Date: Mon, 24 Oct 2022 17:00:00 +0530 Subject: [PATCH 3/6] Update recursive_approach_knapsack.py Updated the code --- knapsack/recursive_approach_knapsack.py | 29 +++++++++++++++---------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/knapsack/recursive_approach_knapsack.py b/knapsack/recursive_approach_knapsack.py index f769e0ba6ae7..d3f746b0c3d1 100644 --- a/knapsack/recursive_approach_knapsack.py +++ b/knapsack/recursive_approach_knapsack.py @@ -17,45 +17,46 @@ """ -def knapsack(weights: list, values: list, n: int, max_weight: int, index: int) -> int: +def knapsack(weights: list, values: list, number_of_items: int, max_weight: int, index: int) -> int: """ Function description is as follows- :param weights: Take a list of weights :param values: Take a list of profits corresponding to the weights + :param number_of_items: number of items available to pick from :param max_weight: Maximum weight that could be carried :param index: the element we are looking at :return: Maximum expected gain - >>> knapsack([1, 2, 4, 5], [5, 4, 8, 6], 4, 15) + >>> knapsack([1, 2, 4, 5], [5, 4, 8, 6], 4, 5, 0) 13 - >>> knapsack([3 ,4 , 5], [10, 9 , 8], 3, 25) + >>> knapsack([3 ,4 , 5], [10, 9 , 8], 3, 25, 0) 27 """ - if index == len(weights): + if index == number_of_items: return 0 ans1 = 0 ans2 = 0 - ans1 = knapsack(weights, values, n, max_weight, index + 1) + ans1 = knapsack(weights, values, number_of_items, max_weight, index + 1) if weights[index] <= max_weight: ans2 = values[index] + knapsack( - weights, values, n, max_weight - weights[index], index + 1 + weights, values, number_of_items, max_weight - weights[index], index + 1 ) return max(ans1, ans2) -def take_input(): +def take_input() -> tuple[list, list, int, int]: """ This function is to take input from the user """ - n = int(input("Input number of items: ")) + number_of_items = int(input("Input number of items: ")) - if n == 0: + if number_of_items == 0: return list(), list(), n, 0 weights = list(map(int, input("Input weights separated by spaces: ").split(" "))) values = list(map(int, input("Input profits separated by spaces: ").split(" "))) max_weight = int(input("Max weight allowed: ")) - return weights, values, n, max_weight + return weights, values, number_of_items, max_weight if __name__ == "__main__": @@ -63,7 +64,11 @@ def take_input(): "Input profits, weights, and then max_weight (all positive ints) separated by " "spaces." ) - weights, values, n, max_weight = take_input() + weights, values, number_of_items, max_weight = take_input() # Function Call - print(knapsack(weights, values, n, max_weight, 0)) + print(knapsack(weights, values, number_of_items, max_weight, 0)) + + import doctest + + doctest.testmod() From c35c64fb0b6a01b17d209dfd7314a9de56a947d3 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 24 Oct 2022 11:30:49 +0000 Subject: [PATCH 4/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- knapsack/recursive_approach_knapsack.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/knapsack/recursive_approach_knapsack.py b/knapsack/recursive_approach_knapsack.py index d3f746b0c3d1..6a1f6f22aeb1 100644 --- a/knapsack/recursive_approach_knapsack.py +++ b/knapsack/recursive_approach_knapsack.py @@ -17,12 +17,14 @@ """ -def knapsack(weights: list, values: list, number_of_items: int, max_weight: int, index: int) -> int: +def knapsack( + weights: list, values: list, number_of_items: int, max_weight: int, index: int +) -> int: """ Function description is as follows- :param weights: Take a list of weights :param values: Take a list of profits corresponding to the weights - :param number_of_items: number of items available to pick from + :param number_of_items: number of items available to pick from :param max_weight: Maximum weight that could be carried :param index: the element we are looking at :return: Maximum expected gain @@ -68,7 +70,7 @@ def take_input() -> tuple[list, list, int, int]: # Function Call print(knapsack(weights, values, number_of_items, max_weight, 0)) - + import doctest doctest.testmod() From 16e9acac57a6da0c9d3e707e13e5e10ef02a5304 Mon Sep 17 00:00:00 2001 From: JatinR05 <71865805+JatinR05@users.noreply.github.com> Date: Mon, 24 Oct 2022 17:04:16 +0530 Subject: [PATCH 5/6] Update recursive_approach_knapsack.py Updated --- knapsack/recursive_approach_knapsack.py | 32 +++---------------------- 1 file changed, 3 insertions(+), 29 deletions(-) diff --git a/knapsack/recursive_approach_knapsack.py b/knapsack/recursive_approach_knapsack.py index 6a1f6f22aeb1..c38012008fa1 100644 --- a/knapsack/recursive_approach_knapsack.py +++ b/knapsack/recursive_approach_knapsack.py @@ -17,14 +17,12 @@ """ -def knapsack( - weights: list, values: list, number_of_items: int, max_weight: int, index: int -) -> int: +def knapsack(weights: list, values: list, number_of_items: int, max_weight: int, index: int) -> int: """ Function description is as follows- :param weights: Take a list of weights :param values: Take a list of profits corresponding to the weights - :param number_of_items: number of items available to pick from + :param number_of_items: number of items available to pick from :param max_weight: Maximum weight that could be carried :param index: the element we are looking at :return: Maximum expected gain @@ -45,32 +43,8 @@ def knapsack( return max(ans1, ans2) -def take_input() -> tuple[list, list, int, int]: - """ - This function is to take input from the user - """ - number_of_items = int(input("Input number of items: ")) - - if number_of_items == 0: - return list(), list(), n, 0 - - weights = list(map(int, input("Input weights separated by spaces: ").split(" "))) - values = list(map(int, input("Input profits separated by spaces: ").split(" "))) - max_weight = int(input("Max weight allowed: ")) - - return weights, values, number_of_items, max_weight - - if __name__ == "__main__": - print( - "Input profits, weights, and then max_weight (all positive ints) separated by " - "spaces." - ) - weights, values, number_of_items, max_weight = take_input() - - # Function Call - print(knapsack(weights, values, number_of_items, max_weight, 0)) - + import doctest doctest.testmod() From b5d03603fde9bef53277eb3e713fbf64796d5912 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 24 Oct 2022 11:35:25 +0000 Subject: [PATCH 6/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- knapsack/recursive_approach_knapsack.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/knapsack/recursive_approach_knapsack.py b/knapsack/recursive_approach_knapsack.py index c38012008fa1..d813981cb79c 100644 --- a/knapsack/recursive_approach_knapsack.py +++ b/knapsack/recursive_approach_knapsack.py @@ -17,12 +17,14 @@ """ -def knapsack(weights: list, values: list, number_of_items: int, max_weight: int, index: int) -> int: +def knapsack( + weights: list, values: list, number_of_items: int, max_weight: int, index: int +) -> int: """ Function description is as follows- :param weights: Take a list of weights :param values: Take a list of profits corresponding to the weights - :param number_of_items: number of items available to pick from + :param number_of_items: number of items available to pick from :param max_weight: Maximum weight that could be carried :param index: the element we are looking at :return: Maximum expected gain @@ -44,7 +46,7 @@ def knapsack(weights: list, values: list, number_of_items: int, max_weight: int, if __name__ == "__main__": - + import doctest doctest.testmod()