From 91e962685a1eb754b8f3ade447234d7629123713 Mon Sep 17 00:00:00 2001 From: Apoorve73 Date: Mon, 8 Jun 2020 16:04:16 +0530 Subject: [PATCH 01/19] Add Greedy Method Approach --- Greedy Method/KnapSack_unittest.py | 13 ++++ Greedy Method/knapsack_problem.py | 100 +++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 Greedy Method/KnapSack_unittest.py create mode 100644 Greedy Method/knapsack_problem.py diff --git a/Greedy Method/KnapSack_unittest.py b/Greedy Method/KnapSack_unittest.py new file mode 100644 index 000000000000..98e7c7f14223 --- /dev/null +++ b/Greedy Method/KnapSack_unittest.py @@ -0,0 +1,13 @@ +import unittest +import knapsack_problem as kp + +class TestClass(unittest.TestCase): + + def test_sorted(self): + gain = kp.calc_Profit([10,20,30,40,50,60], [2, 4, 6, 8, 10, 12],100) + # self.assertTrue(result) + self.assertEqual(gain,75) + + +if __name__ == "__main__": + unittest.main() \ No newline at end of file diff --git a/Greedy Method/knapsack_problem.py b/Greedy Method/knapsack_problem.py new file mode 100644 index 000000000000..8abdc832294e --- /dev/null +++ b/Greedy Method/knapsack_problem.py @@ -0,0 +1,100 @@ +# Knapsack is a common Greedy Algo problem. +""" +You have bags of wheat with different weights and a different profits for each. +eg. +profit 5 8 7 1 12 3 4 +weight 2 7 1 6 4 2 5 +max_weight 100 +Calculate the max profit the shopkeeper can make for the given max_weight that could be carried. +""" + +# Importing copy module for deepcopy operations +import copy + + +def calc_Profit(profit: list, weight: list, max_weight: int) -> int: + """ + Function description is as follows- + :param profit: Take a list of profits + :param weight: Take a list of weight if bags corresponding to the profits + :param max_weight: Max. weight that could be carried + :return: Max expected gain + + >>> calc_Profit([1,2,3], [3,4,5], 15) + 6 + >>> calc_Profit([10, 9 , 8], [3 ,4 , 5], 25) + 27 + + """ + + # List created to store profit gained for the 1kg in case of each weight + # respectively + prof_by_wei = list() + + # Calculate and append profit/weight for each + for i in range(len(weight)): + prof_by_wei.append(profit[i] / weight[i]) + + # Creating a copy of the list and sorting proit/weight in ascending order + temp_pbw = sorted(copy.deepcopy(prof_by_wei)) + + # declaring useful variables + l = len(temp_pbw) + limit = 0 + gain = 0 + i = 0 + + # loop till the total weight do not reach max limit i.e. 15 kg and till i= weight[index]: + + limit += weight[index] + # Adding profit gained for the given weight 1 === + # weight[index]/weight[index] + gain += 1 * profit[index] + + else: + # Since the weight encountered is greater than limit, therefore take the required number of remaining kgs and calculate profit for it. + # weight remaining/ weight[index] + gain += ((15 - limit) / weight[index]) * profit[index] + break + i += 1 + + return gain + + +if __name__ == "__main__": + + # Input profit, weight and max_weight (all positive values). + + profit = [int(x) for x in input().split()] + weight = [int(x) for x in input().split()] + max_weight = int(input()) + + if len(profit) != len(weight): + print("The length of both the arrays must be same! Try again!") + + if max_weight < 0: + print("Gotcha! Weight is a positive quantity") + + for i in range(len(profit)): + if (profit[i] or weight[i]) < 0: + print("Ono! Input positive values only! Better luck next time") + break + else: + calc_Profit(profit, weight, max_weight) From a3b265f94cbedf35a277e324d4fc0b688749e18e Mon Sep 17 00:00:00 2001 From: Apoorve73 Date: Mon, 8 Jun 2020 16:14:32 +0530 Subject: [PATCH 02/19] Update Filename --- .../KnapSack_unittest.py => greedy _method/knapSack_unittest.py | 0 {Greedy Method => greedy _method}/knapsack_problem.py | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename Greedy Method/KnapSack_unittest.py => greedy _method/knapSack_unittest.py (100%) rename {Greedy Method => greedy _method}/knapsack_problem.py (100%) diff --git a/Greedy Method/KnapSack_unittest.py b/greedy _method/knapSack_unittest.py similarity index 100% rename from Greedy Method/KnapSack_unittest.py rename to greedy _method/knapSack_unittest.py diff --git a/Greedy Method/knapsack_problem.py b/greedy _method/knapsack_problem.py similarity index 100% rename from Greedy Method/knapsack_problem.py rename to greedy _method/knapsack_problem.py From 7205ad5c55ac6cdc2ac6138378951393e108c70e Mon Sep 17 00:00:00 2001 From: Apoorve73 Date: Mon, 8 Jun 2020 16:22:33 +0530 Subject: [PATCH 03/19] Update Variable and Links --- .../knapSack_unittest.py | 0 .../knapsack_problem.py | 39 ++++++++++--------- 2 files changed, 21 insertions(+), 18 deletions(-) rename {greedy _method => greedy_method}/knapSack_unittest.py (100%) rename {greedy _method => greedy_method}/knapsack_problem.py (70%) diff --git a/greedy _method/knapSack_unittest.py b/greedy_method/knapSack_unittest.py similarity index 100% rename from greedy _method/knapSack_unittest.py rename to greedy_method/knapSack_unittest.py diff --git a/greedy _method/knapsack_problem.py b/greedy_method/knapsack_problem.py similarity index 70% rename from greedy _method/knapsack_problem.py rename to greedy_method/knapsack_problem.py index 8abdc832294e..ca6988d461c9 100644 --- a/greedy _method/knapsack_problem.py +++ b/greedy_method/knapsack_problem.py @@ -1,23 +1,26 @@ +# Refer https://en.wikipedia.org/wiki/Greedy_algorithm#:~:text=A%20greedy%20algorithm%20is%20any,of%20finding%20a%20global%20optimum. +# to get an insight into Greedy Algorithm + # Knapsack is a common Greedy Algo problem. """ You have bags of wheat with different weights and a different profits for each. eg. profit 5 8 7 1 12 3 4 weight 2 7 1 6 4 2 5 -max_weight 100 -Calculate the max profit the shopkeeper can make for the given max_weight that could be carried. +max_Weight 100 +Calculate the max profit the shopkeeper can make for the given max_Weight that could be carried. """ # Importing copy module for deepcopy operations import copy -def calc_Profit(profit: list, weight: list, max_weight: int) -> int: +def calc_Profit(profit: list, weight: list, max_Weight: int) -> int: """ Function description is as follows- :param profit: Take a list of profits :param weight: Take a list of weight if bags corresponding to the profits - :param max_weight: Max. weight that could be carried + :param max_Weight: Max. weight that could be carried :return: Max expected gain >>> calc_Profit([1,2,3], [3,4,5], 15) @@ -29,17 +32,17 @@ def calc_Profit(profit: list, weight: list, max_weight: int) -> int: # List created to store profit gained for the 1kg in case of each weight # respectively - prof_by_wei = list() + profit_By_Weight = list() # Calculate and append profit/weight for each for i in range(len(weight)): - prof_by_wei.append(profit[i] / weight[i]) + profit_By_Weight.append(profit[i] / weight[i]) # Creating a copy of the list and sorting proit/weight in ascending order - temp_pbw = sorted(copy.deepcopy(prof_by_wei)) + temp_PBW = sorted(copy.deepcopy(profit_By_Weight)) # declaring useful variables - l = len(temp_pbw) + l = len(temp_PBW) limit = 0 gain = 0 i = 0 @@ -47,17 +50,17 @@ def calc_Profit(profit: list, weight: list, max_weight: int) -> int: # loop till the total weight do not reach max limit i.e. 15 kg and till i int: if __name__ == "__main__": - # Input profit, weight and max_weight (all positive values). + # Input profit, weight and max_Weight (all positive values). profit = [int(x) for x in input().split()] weight = [int(x) for x in input().split()] - max_weight = int(input()) + max_Weight = int(input()) if len(profit) != len(weight): print("The length of both the arrays must be same! Try again!") - if max_weight < 0: + if max_Weight < 0: print("Gotcha! Weight is a positive quantity") for i in range(len(profit)): @@ -97,4 +100,4 @@ def calc_Profit(profit: list, weight: list, max_weight: int) -> int: print("Ono! Input positive values only! Better luck next time") break else: - calc_Profit(profit, weight, max_weight) + calc_Profit(profit, weight, max_Weight) From 746d19ff6326dff8598ed2498dd56120e61b42fb Mon Sep 17 00:00:00 2001 From: Apoorve73 Date: Mon, 8 Jun 2020 17:01:51 +0530 Subject: [PATCH 04/19] Fixed flake8 bugs --- greedy_method/knapSack_unittest.py | 14 +++++++++++--- greedy_method/knapsack_problem.py | 19 ++++++++++--------- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/greedy_method/knapSack_unittest.py b/greedy_method/knapSack_unittest.py index 98e7c7f14223..e177864fd93c 100644 --- a/greedy_method/knapSack_unittest.py +++ b/greedy_method/knapSack_unittest.py @@ -1,13 +1,21 @@ import unittest import knapsack_problem as kp + class TestClass(unittest.TestCase): + """ + Test cases for knapsack_problem + """ def test_sorted(self): - gain = kp.calc_Profit([10,20,30,40,50,60], [2, 4, 6, 8, 10, 12],100) + """ + kp.calc_Profit takes the required argument (profit, weight, max_weight) + and returns whether the answer matches to the expected ones + """ + gain = kp.calc_Profit([10, 20, 30, 40, 50, 60], [2, 4, 6, 8, 10, 12], 100) # self.assertTrue(result) - self.assertEqual(gain,75) + self.assertEqual(gain, 75) if __name__ == "__main__": - unittest.main() \ No newline at end of file + unittest.main() diff --git a/greedy_method/knapsack_problem.py b/greedy_method/knapsack_problem.py index ca6988d461c9..f07592c02519 100644 --- a/greedy_method/knapsack_problem.py +++ b/greedy_method/knapsack_problem.py @@ -1,7 +1,7 @@ -# Refer https://en.wikipedia.org/wiki/Greedy_algorithm#:~:text=A%20greedy%20algorithm%20is%20any,of%20finding%20a%20global%20optimum. -# to get an insight into Greedy Algorithm +# To get an insight into Greedy Algorithm +# Knapsack is a common Greedy Algo problem + -# Knapsack is a common Greedy Algo problem. """ You have bags of wheat with different weights and a different profits for each. eg. @@ -42,22 +42,22 @@ def calc_Profit(profit: list, weight: list, max_Weight: int) -> int: temp_PBW = sorted(copy.deepcopy(profit_By_Weight)) # declaring useful variables - l = len(temp_PBW) + length = len(temp_PBW) limit = 0 gain = 0 i = 0 # loop till the total weight do not reach max limit i.e. 15 kg and till i int: gain += 1 * profit[index] else: - # Since the weight encountered is greater than limit, therefore take the required number of remaining kgs and calculate profit for it. + # Since the weight encountered is greater than limit, therefore take the required number of remaining kgs + # and calculate profit for it. # weight remaining/ weight[index] gain += ((15 - limit) / weight[index]) * profit[index] break From d4c652db7cb0241ad1cedb65907153408438766e Mon Sep 17 00:00:00 2001 From: Apoorve73 Date: Mon, 8 Jun 2020 17:09:46 +0530 Subject: [PATCH 05/19] Update unittest filename --- greedy_method/{knapSack_unittest.py => knapsack_unittest.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename greedy_method/{knapSack_unittest.py => knapsack_unittest.py} (100%) diff --git a/greedy_method/knapSack_unittest.py b/greedy_method/knapsack_unittest.py similarity index 100% rename from greedy_method/knapSack_unittest.py rename to greedy_method/knapsack_unittest.py From 33318b4b592983242330add9e298d5cd479847ff Mon Sep 17 00:00:00 2001 From: Apoorve73 Date: Mon, 8 Jun 2020 17:54:58 +0530 Subject: [PATCH 06/19] Update unittest filename --- greedy_method/{knapsack_unittest.py => test_*.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename greedy_method/{knapsack_unittest.py => test_*.py} (100%) diff --git a/greedy_method/knapsack_unittest.py b/greedy_method/test_*.py similarity index 100% rename from greedy_method/knapsack_unittest.py rename to greedy_method/test_*.py From 8dc157da18d94156e1c057030b868da6a5a7d513 Mon Sep 17 00:00:00 2001 From: Apoorve73 Date: Mon, 8 Jun 2020 18:36:01 +0530 Subject: [PATCH 07/19] Final unittest filename update --- greedy_method/{test_*.py => test_knapsack.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename greedy_method/{test_*.py => test_knapsack.py} (100%) diff --git a/greedy_method/test_*.py b/greedy_method/test_knapsack.py similarity index 100% rename from greedy_method/test_*.py rename to greedy_method/test_knapsack.py From 8a112aeedb177a6122b30c23ac586026c7a4a1b9 Mon Sep 17 00:00:00 2001 From: Apoorve73 Date: Tue, 9 Jun 2020 15:59:27 +0530 Subject: [PATCH 08/19] Pythonic Code formatting --- greedy_method/knapsack_problem.py | 74 ++++++++++++++++++------------- greedy_method/test_knapsack.py | 70 +++++++++++++++++++++++++++-- 2 files changed, 111 insertions(+), 33 deletions(-) diff --git a/greedy_method/knapsack_problem.py b/greedy_method/knapsack_problem.py index f07592c02519..38b74eaf712e 100644 --- a/greedy_method/knapsack_problem.py +++ b/greedy_method/knapsack_problem.py @@ -8,14 +8,17 @@ profit 5 8 7 1 12 3 4 weight 2 7 1 6 4 2 5 max_Weight 100 + +Constraints: +Max_Weight > 0 +profit[i] >= 0 +weight[i] >= 0 Calculate the max profit the shopkeeper can make for the given max_Weight that could be carried. """ - -# Importing copy module for deepcopy operations -import copy +from typing import Union -def calc_Profit(profit: list, weight: list, max_Weight: int) -> int: +def Calc_Profit(profit: list, weight: list, max_Weight: int) -> Union[str, int]: """ Function description is as follows- :param profit: Take a list of profits @@ -23,12 +26,31 @@ def calc_Profit(profit: list, weight: list, max_Weight: int) -> int: :param max_Weight: Max. weight that could be carried :return: Max expected gain - >>> calc_Profit([1,2,3], [3,4,5], 15) + >>> Calc_Profit([1,2,3], [3,4,5], 15) 6 - >>> calc_Profit([10, 9 , 8], [3 ,4 , 5], 25) + >>> Calc_Profit([10, 9 , 8], [3 ,4 , 5], 25) 27 - """ + if len(profit) != len(weight): + raise IndexError( + "<< The length of both the arrays must be same! Try again.. >>" + ) + + if max_Weight <= 0: + raise ValueError( + "<< Gotcha! Max_Weight is a positive quantity greater than zero! >>" + ) + + for i in range(len(profit)): + if profit[i] < 0: + raise ValueError( + "<< Ono! Profit means positive value. Better luck next time! >>" + ) + + if weight[i] < 0: + raise ValueError( + "<< Oops! Could not accept a negative value for weight. Try Again.. >>" + ) # List created to store profit gained for the 1kg in case of each weight # respectively @@ -38,8 +60,8 @@ def calc_Profit(profit: list, weight: list, max_Weight: int) -> int: for i in range(len(weight)): profit_By_Weight.append(profit[i] / weight[i]) - # Creating a copy of the list and sorting proit/weight in ascending order - temp_PBW = sorted(copy.deepcopy(profit_By_Weight)) + # Creating a copy of the list and sorting profit/weight in ascending order + temp_PBW = sorted(profit_By_Weight) # declaring useful variables length = len(temp_PBW) @@ -47,15 +69,16 @@ def calc_Profit(profit: list, weight: list, max_Weight: int) -> int: gain = 0 i = 0 - # loop till the total weight do not reach max limit i.e. 15 kg and till i int: # check if the weight encountered is less than the total weight # encountered before. - if 15 - limit >= weight[index]: + if max_Weight - limit >= weight[index]: limit += weight[index] # Adding profit gained for the given weight 1 === @@ -72,10 +95,11 @@ def calc_Profit(profit: list, weight: list, max_Weight: int) -> int: gain += 1 * profit[index] else: - # Since the weight encountered is greater than limit, therefore take the required number of remaining kgs - # and calculate profit for it. + # Since the weight encountered is greater than limit, therefore take the required number of + # remaining kgs + # and Calculate profit for it. # weight remaining/ weight[index] - gain += ((15 - limit) / weight[index]) * profit[index] + gain += ((max_Weight - limit) / weight[index]) * profit[index] break i += 1 @@ -88,17 +112,7 @@ def calc_Profit(profit: list, weight: list, max_Weight: int) -> int: profit = [int(x) for x in input().split()] weight = [int(x) for x in input().split()] - max_Weight = int(input()) - - if len(profit) != len(weight): - print("The length of both the arrays must be same! Try again!") - - if max_Weight < 0: - print("Gotcha! Weight is a positive quantity") + max_Weight = int(input("Max weight allowed: ")) - for i in range(len(profit)): - if (profit[i] or weight[i]) < 0: - print("Ono! Input positive values only! Better luck next time") - break - else: - calc_Profit(profit, weight, max_Weight) + # Function Call + Calc_Profit(profit, weight, max_Weight) diff --git a/greedy_method/test_knapsack.py b/greedy_method/test_knapsack.py index e177864fd93c..a69ad04cadf2 100644 --- a/greedy_method/test_knapsack.py +++ b/greedy_method/test_knapsack.py @@ -12,9 +12,73 @@ def test_sorted(self): kp.calc_Profit takes the required argument (profit, weight, max_weight) and returns whether the answer matches to the expected ones """ - gain = kp.calc_Profit([10, 20, 30, 40, 50, 60], [2, 4, 6, 8, 10, 12], 100) - # self.assertTrue(result) - self.assertEqual(gain, 75) + profit = [10, 20, 30, 40, 50, 60] + weight = [2, 4, 6, 8, 10, 12] + max_Weight = 100 + self.assertEqual(kp.Calc_Profit(profit, weight, max_Weight), 210) + + def test_negative_maxWeight(self): + """ + Returns ValueError for any negative max_weight value + :return: ValueError + """ + profit = [10, 20, 30, 40, 50, 60] + weight = [2, 4, 6, 8, 10, 12] + max_Weight = -15 + self.assertRaisesRegex( + ValueError, + "<< Gotcha! Max_Weight is a positive quantity greater than zero! >>", + ) + + def test_negative_profit_Value(self): + """ + Returns ValueError for any negative profit value in the list + :return: ValueError + """ + profit = [10, -20, 30, 40, 50, 60] + weight = [2, 4, 6, 8, 10, 12] + max_Weight = 15 + self.assertRaisesRegex( + ValueError, + "<< Oops! Could not accept a negative value for weight. Try Again.. >>", + ) + + def test_negative_weight_Value(self): + """ + Returns ValueError for any negative weight value in the list + :return: ValueError + """ + profit = [10, 20, 30, 40, 50, 60] + weight = [2, -4, 6, -8, 10, 12] + max_Weight = 15 + self.assertRaisesRegex( + ValueError, "<< Ono! Profit means positive value. Better luck next time! >>" + ) + + def test_zero_maxWeight(self): + """ + Returns ValueError for any zero max_weight value + :return: ValueError + """ + profit = [10, 20, 30, 40, 50, 60] + weight = [2, 4, 6, 8, 10, 12] + max_Weight = 0 + self.assertRaisesRegex( + ValueError, + "<< Gotcha! Max_Weight is a positive quantity greater than zero! >>", + ) + + def test_unequal_list_length(self): + """ + Returns IndexError if length of lists (profit and weight) are unequal. + :return: IndexError + """ + profit = [10, 20, 30, 40, 50] + weight = [2, 4, 6, 8, 10, 12] + max_Weight = 100 + self.assertRaisesRegex( + IndexError, "<< The length of both the arrays must be same! Try again.. >>" + ) if __name__ == "__main__": From 6be08f5ff7c66bf2c1cd0da62fdf8e1b6e8b66e8 Mon Sep 17 00:00:00 2001 From: Apoorve73 Date: Tue, 9 Jun 2020 16:03:40 +0530 Subject: [PATCH 09/19] flake8 fixes --- greedy_method/test_knapsack.py | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/greedy_method/test_knapsack.py b/greedy_method/test_knapsack.py index a69ad04cadf2..6228b600b564 100644 --- a/greedy_method/test_knapsack.py +++ b/greedy_method/test_knapsack.py @@ -22,9 +22,9 @@ def test_negative_maxWeight(self): Returns ValueError for any negative max_weight value :return: ValueError """ - profit = [10, 20, 30, 40, 50, 60] - weight = [2, 4, 6, 8, 10, 12] - max_Weight = -15 + # profit = [10, 20, 30, 40, 50, 60] + # weight = [2, 4, 6, 8, 10, 12] + # max_Weight = -15 self.assertRaisesRegex( ValueError, "<< Gotcha! Max_Weight is a positive quantity greater than zero! >>", @@ -35,9 +35,9 @@ def test_negative_profit_Value(self): Returns ValueError for any negative profit value in the list :return: ValueError """ - profit = [10, -20, 30, 40, 50, 60] - weight = [2, 4, 6, 8, 10, 12] - max_Weight = 15 + # profit = [10, -20, 30, 40, 50, 60] + # weight = [2, 4, 6, 8, 10, 12] + # max_Weight = 15 self.assertRaisesRegex( ValueError, "<< Oops! Could not accept a negative value for weight. Try Again.. >>", @@ -48,9 +48,9 @@ def test_negative_weight_Value(self): Returns ValueError for any negative weight value in the list :return: ValueError """ - profit = [10, 20, 30, 40, 50, 60] - weight = [2, -4, 6, -8, 10, 12] - max_Weight = 15 + # profit = [10, 20, 30, 40, 50, 60] + # weight = [2, -4, 6, -8, 10, 12] + # max_Weight = 15 self.assertRaisesRegex( ValueError, "<< Ono! Profit means positive value. Better luck next time! >>" ) @@ -60,9 +60,9 @@ def test_zero_maxWeight(self): Returns ValueError for any zero max_weight value :return: ValueError """ - profit = [10, 20, 30, 40, 50, 60] - weight = [2, 4, 6, 8, 10, 12] - max_Weight = 0 + # profit = [10, 20, 30, 40, 50, 60] + # weight = [2, 4, 6, 8, 10, 12] + # max_Weight = 0 self.assertRaisesRegex( ValueError, "<< Gotcha! Max_Weight is a positive quantity greater than zero! >>", @@ -73,9 +73,9 @@ def test_unequal_list_length(self): Returns IndexError if length of lists (profit and weight) are unequal. :return: IndexError """ - profit = [10, 20, 30, 40, 50] - weight = [2, 4, 6, 8, 10, 12] - max_Weight = 100 + # profit = [10, 20, 30, 40, 50] + # weight = [2, 4, 6, 8, 10, 12] + # max_Weight = 100 self.assertRaisesRegex( IndexError, "<< The length of both the arrays must be same! Try again.. >>" ) From 38652c08623e8b1f9c479da8a921582036c7cdde Mon Sep 17 00:00:00 2001 From: Apoorve73 Date: Tue, 9 Jun 2020 17:23:40 +0530 Subject: [PATCH 10/19] lowercase function name --- greedy_method/knapsack_problem.py | 8 ++++---- greedy_method/test_knapsack.py | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/greedy_method/knapsack_problem.py b/greedy_method/knapsack_problem.py index 38b74eaf712e..4146f9d36592 100644 --- a/greedy_method/knapsack_problem.py +++ b/greedy_method/knapsack_problem.py @@ -18,7 +18,7 @@ from typing import Union -def Calc_Profit(profit: list, weight: list, max_Weight: int) -> Union[str, int]: +def calc_profit(profit: list, weight: list, max_Weight: int) -> Union[str, int]: """ Function description is as follows- :param profit: Take a list of profits @@ -26,9 +26,9 @@ def Calc_Profit(profit: list, weight: list, max_Weight: int) -> Union[str, int]: :param max_Weight: Max. weight that could be carried :return: Max expected gain - >>> Calc_Profit([1,2,3], [3,4,5], 15) + >>> calc_profit([1,2,3], [3,4,5], 15) 6 - >>> Calc_Profit([10, 9 , 8], [3 ,4 , 5], 25) + >>> calc_profit([10, 9 , 8], [3 ,4 , 5], 25) 27 """ if len(profit) != len(weight): @@ -115,4 +115,4 @@ def Calc_Profit(profit: list, weight: list, max_Weight: int) -> Union[str, int]: max_Weight = int(input("Max weight allowed: ")) # Function Call - Calc_Profit(profit, weight, max_Weight) + calc_profit(profit, weight, max_Weight) diff --git a/greedy_method/test_knapsack.py b/greedy_method/test_knapsack.py index 6228b600b564..4b6738a4e214 100644 --- a/greedy_method/test_knapsack.py +++ b/greedy_method/test_knapsack.py @@ -9,13 +9,13 @@ class TestClass(unittest.TestCase): def test_sorted(self): """ - kp.calc_Profit takes the required argument (profit, weight, max_weight) + kp.calc_profit takes the required argument (profit, weight, max_weight) and returns whether the answer matches to the expected ones """ profit = [10, 20, 30, 40, 50, 60] weight = [2, 4, 6, 8, 10, 12] max_Weight = 100 - self.assertEqual(kp.Calc_Profit(profit, weight, max_Weight), 210) + self.assertEqual(kp.calc_profit(profit, weight, max_Weight), 210) def test_negative_maxWeight(self): """ From 0d0b2fc399cbfd4dcc737b374b2a6d3c770eaebb Mon Sep 17 00:00:00 2001 From: Apoorve73 Date: Tue, 9 Jun 2020 17:31:00 +0530 Subject: [PATCH 11/19] Add zip function --- greedy_method/knapsack_problem.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/greedy_method/knapsack_problem.py b/greedy_method/knapsack_problem.py index 4146f9d36592..bf9035c5d434 100644 --- a/greedy_method/knapsack_problem.py +++ b/greedy_method/knapsack_problem.py @@ -56,9 +56,8 @@ def calc_profit(profit: list, weight: list, max_Weight: int) -> Union[str, int]: # respectively profit_By_Weight = list() - # Calculate and append profit/weight for each - for i in range(len(weight)): - profit_By_Weight.append(profit[i] / weight[i]) + # Calculate and append profit/weight for each element + profit_by_weight = [p / w for p, w in zip(profit, weight)] # Creating a copy of the list and sorting profit/weight in ascending order temp_PBW = sorted(profit_By_Weight) From 60335332a2e135c33966c46e468126eb2987ecac Mon Sep 17 00:00:00 2001 From: Apoorve73 Date: Tue, 9 Jun 2020 17:31:35 +0530 Subject: [PATCH 12/19] Add zip function --- greedy_method/knapsack_problem.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/greedy_method/knapsack_problem.py b/greedy_method/knapsack_problem.py index bf9035c5d434..c6f4d5fa7b72 100644 --- a/greedy_method/knapsack_problem.py +++ b/greedy_method/knapsack_problem.py @@ -57,7 +57,7 @@ def calc_profit(profit: list, weight: list, max_Weight: int) -> Union[str, int]: profit_By_Weight = list() # Calculate and append profit/weight for each element - profit_by_weight = [p / w for p, w in zip(profit, weight)] + profit_by_Weight = [p / w for p, w in zip(profit, weight)] # Creating a copy of the list and sorting profit/weight in ascending order temp_PBW = sorted(profit_By_Weight) From 151e2fdcdd9020b3cee501f458b939fe2f3ae1eb Mon Sep 17 00:00:00 2001 From: Apoorve73 Date: Tue, 9 Jun 2020 17:35:30 +0530 Subject: [PATCH 13/19] params lowercase --- greedy_method/knapsack_problem.py | 28 ++++++++++++++-------------- greedy_method/test_knapsack.py | 18 +++++++++--------- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/greedy_method/knapsack_problem.py b/greedy_method/knapsack_problem.py index c6f4d5fa7b72..b9eef79207a3 100644 --- a/greedy_method/knapsack_problem.py +++ b/greedy_method/knapsack_problem.py @@ -7,23 +7,23 @@ eg. profit 5 8 7 1 12 3 4 weight 2 7 1 6 4 2 5 -max_Weight 100 +max_weight 100 Constraints: -Max_Weight > 0 +max_weight > 0 profit[i] >= 0 weight[i] >= 0 -Calculate the max profit the shopkeeper can make for the given max_Weight that could be carried. +Calculate the max profit the shopkeeper can make for the given max_weight that could be carried. """ from typing import Union -def calc_profit(profit: list, weight: list, max_Weight: int) -> Union[str, int]: +def calc_profit(profit: list, weight: list, max_weight: int) -> Union[str, int]: """ Function description is as follows- :param profit: Take a list of profits :param weight: Take a list of weight if bags corresponding to the profits - :param max_Weight: Max. weight that could be carried + :param max_weight: Max. weight that could be carried :return: Max expected gain >>> calc_profit([1,2,3], [3,4,5], 15) @@ -36,9 +36,9 @@ def calc_profit(profit: list, weight: list, max_Weight: int) -> Union[str, int]: "<< The length of both the arrays must be same! Try again.. >>" ) - if max_Weight <= 0: + if max_weight <= 0: raise ValueError( - "<< Gotcha! Max_Weight is a positive quantity greater than zero! >>" + "<< Gotcha! max_weight is a positive quantity greater than zero! >>" ) for i in range(len(profit)): @@ -57,7 +57,7 @@ def calc_profit(profit: list, weight: list, max_Weight: int) -> Union[str, int]: profit_By_Weight = list() # Calculate and append profit/weight for each element - profit_by_Weight = [p / w for p, w in zip(profit, weight)] + profit_By_Weight = [p / w for p, w in zip(profit, weight)] # Creating a copy of the list and sorting profit/weight in ascending order temp_PBW = sorted(profit_By_Weight) @@ -69,7 +69,7 @@ def calc_profit(profit: list, weight: list, max_Weight: int) -> Union[str, int]: i = 0 # loop till the total weight do not reach max limit e.g. 15 kg and till i Union[str, int]: # check if the weight encountered is less than the total weight # encountered before. - if max_Weight - limit >= weight[index]: + if max_weight - limit >= weight[index]: limit += weight[index] # Adding profit gained for the given weight 1 === @@ -98,7 +98,7 @@ def calc_profit(profit: list, weight: list, max_Weight: int) -> Union[str, int]: # remaining kgs # and Calculate profit for it. # weight remaining/ weight[index] - gain += ((max_Weight - limit) / weight[index]) * profit[index] + gain += ((max_weight - limit) / weight[index]) * profit[index] break i += 1 @@ -107,11 +107,11 @@ def calc_profit(profit: list, weight: list, max_Weight: int) -> Union[str, int]: if __name__ == "__main__": - # Input profit, weight and max_Weight (all positive values). + # Input profit, weight and max_weight (all positive values). profit = [int(x) for x in input().split()] weight = [int(x) for x in input().split()] - max_Weight = int(input("Max weight allowed: ")) + max_weight = int(input("Max weight allowed: ")) # Function Call - calc_profit(profit, weight, max_Weight) + calc_profit(profit, weight, max_weight) diff --git a/greedy_method/test_knapsack.py b/greedy_method/test_knapsack.py index 4b6738a4e214..6414de407810 100644 --- a/greedy_method/test_knapsack.py +++ b/greedy_method/test_knapsack.py @@ -14,8 +14,8 @@ def test_sorted(self): """ profit = [10, 20, 30, 40, 50, 60] weight = [2, 4, 6, 8, 10, 12] - max_Weight = 100 - self.assertEqual(kp.calc_profit(profit, weight, max_Weight), 210) + max_weight = 100 + self.assertEqual(kp.calc_profit(profit, weight, max_weight), 210) def test_negative_maxWeight(self): """ @@ -24,10 +24,10 @@ def test_negative_maxWeight(self): """ # profit = [10, 20, 30, 40, 50, 60] # weight = [2, 4, 6, 8, 10, 12] - # max_Weight = -15 + # max_weight = -15 self.assertRaisesRegex( ValueError, - "<< Gotcha! Max_Weight is a positive quantity greater than zero! >>", + "<< Gotcha! max_weight is a positive quantity greater than zero! >>", ) def test_negative_profit_Value(self): @@ -37,7 +37,7 @@ def test_negative_profit_Value(self): """ # profit = [10, -20, 30, 40, 50, 60] # weight = [2, 4, 6, 8, 10, 12] - # max_Weight = 15 + # max_weight = 15 self.assertRaisesRegex( ValueError, "<< Oops! Could not accept a negative value for weight. Try Again.. >>", @@ -50,7 +50,7 @@ def test_negative_weight_Value(self): """ # profit = [10, 20, 30, 40, 50, 60] # weight = [2, -4, 6, -8, 10, 12] - # max_Weight = 15 + # max_weight = 15 self.assertRaisesRegex( ValueError, "<< Ono! Profit means positive value. Better luck next time! >>" ) @@ -62,10 +62,10 @@ def test_zero_maxWeight(self): """ # profit = [10, 20, 30, 40, 50, 60] # weight = [2, 4, 6, 8, 10, 12] - # max_Weight = 0 + # max_weight = 0 self.assertRaisesRegex( ValueError, - "<< Gotcha! Max_Weight is a positive quantity greater than zero! >>", + "<< Gotcha! max_weight is a positive quantity greater than zero! >>", ) def test_unequal_list_length(self): @@ -75,7 +75,7 @@ def test_unequal_list_length(self): """ # profit = [10, 20, 30, 40, 50] # weight = [2, 4, 6, 8, 10, 12] - # max_Weight = 100 + # max_weight = 100 self.assertRaisesRegex( IndexError, "<< The length of both the arrays must be same! Try again.. >>" ) From 5b862d9d859cf7250b2e7b9ab5bad014ada9111f Mon Sep 17 00:00:00 2001 From: Apoorve73 Date: Tue, 9 Jun 2020 17:54:02 +0530 Subject: [PATCH 14/19] Travis CI fixes --- greedy_method/knapsack_problem.py | 26 +++++++++++++------------- greedy_method/test_knapsack.py | 26 +++++++++++++------------- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/greedy_method/knapsack_problem.py b/greedy_method/knapsack_problem.py index b9eef79207a3..1194f7fd3541 100644 --- a/greedy_method/knapsack_problem.py +++ b/greedy_method/knapsack_problem.py @@ -7,23 +7,23 @@ eg. profit 5 8 7 1 12 3 4 weight 2 7 1 6 4 2 5 -max_weight 100 +max_Weight 100 Constraints: -max_weight > 0 +max_Weight > 0 profit[i] >= 0 weight[i] >= 0 -Calculate the max profit the shopkeeper can make for the given max_weight that could be carried. +Calculate the max profit the shopkeeper can make for the given max_Weight that could be carried. """ from typing import Union -def calc_profit(profit: list, weight: list, max_weight: int) -> Union[str, int]: +def calc_profit(profit: list, weight: list, max_Weight: int) -> Union[str, int]: """ Function description is as follows- :param profit: Take a list of profits :param weight: Take a list of weight if bags corresponding to the profits - :param max_weight: Max. weight that could be carried + :param max_Weight: Max. weight that could be carried :return: Max expected gain >>> calc_profit([1,2,3], [3,4,5], 15) @@ -36,9 +36,9 @@ def calc_profit(profit: list, weight: list, max_weight: int) -> Union[str, int]: "<< The length of both the arrays must be same! Try again.. >>" ) - if max_weight <= 0: + if max_Weight <= 0: raise ValueError( - "<< Gotcha! max_weight is a positive quantity greater than zero! >>" + "<< Gotcha! max_Weight is a positive quantity greater than zero! >>" ) for i in range(len(profit)): @@ -69,7 +69,7 @@ def calc_profit(profit: list, weight: list, max_weight: int) -> Union[str, int]: i = 0 # loop till the total weight do not reach max limit e.g. 15 kg and till i Union[str, int]: # check if the weight encountered is less than the total weight # encountered before. - if max_weight - limit >= weight[index]: + if max_Weight - limit >= weight[index]: limit += weight[index] # Adding profit gained for the given weight 1 === @@ -98,7 +98,7 @@ def calc_profit(profit: list, weight: list, max_weight: int) -> Union[str, int]: # remaining kgs # and Calculate profit for it. # weight remaining/ weight[index] - gain += ((max_weight - limit) / weight[index]) * profit[index] + gain += ((max_Weight - limit) / weight[index]) * profit[index] break i += 1 @@ -107,11 +107,11 @@ def calc_profit(profit: list, weight: list, max_weight: int) -> Union[str, int]: if __name__ == "__main__": - # Input profit, weight and max_weight (all positive values). + # Input profit, weight and max_Weight (all positive values). profit = [int(x) for x in input().split()] weight = [int(x) for x in input().split()] - max_weight = int(input("Max weight allowed: ")) + max_Weight = int(input("Max weight allowed: ")) # Function Call - calc_profit(profit, weight, max_weight) + calc_profit(profit, weight, max_Weight) diff --git a/greedy_method/test_knapsack.py b/greedy_method/test_knapsack.py index 6414de407810..41fb56372467 100644 --- a/greedy_method/test_knapsack.py +++ b/greedy_method/test_knapsack.py @@ -9,25 +9,25 @@ class TestClass(unittest.TestCase): def test_sorted(self): """ - kp.calc_profit takes the required argument (profit, weight, max_weight) + kp.calc_profit takes the required argument (profit, weight, max_Weight) and returns whether the answer matches to the expected ones """ profit = [10, 20, 30, 40, 50, 60] weight = [2, 4, 6, 8, 10, 12] - max_weight = 100 - self.assertEqual(kp.calc_profit(profit, weight, max_weight), 210) + max_Weight = 100 + self.assertEqual(kp.calc_profit(profit, weight, max_Weight), 210) def test_negative_maxWeight(self): """ - Returns ValueError for any negative max_weight value + Returns ValueError for any negative max_Weight value :return: ValueError """ # profit = [10, 20, 30, 40, 50, 60] # weight = [2, 4, 6, 8, 10, 12] - # max_weight = -15 + # max_Weight = -15 self.assertRaisesRegex( ValueError, - "<< Gotcha! max_weight is a positive quantity greater than zero! >>", + "<< Gotcha! max_Weight is a positive quantity greater than zero! >>", ) def test_negative_profit_Value(self): @@ -37,7 +37,7 @@ def test_negative_profit_Value(self): """ # profit = [10, -20, 30, 40, 50, 60] # weight = [2, 4, 6, 8, 10, 12] - # max_weight = 15 + # max_Weight = 15 self.assertRaisesRegex( ValueError, "<< Oops! Could not accept a negative value for weight. Try Again.. >>", @@ -50,22 +50,22 @@ def test_negative_weight_Value(self): """ # profit = [10, 20, 30, 40, 50, 60] # weight = [2, -4, 6, -8, 10, 12] - # max_weight = 15 + # max_Weight = 15 self.assertRaisesRegex( ValueError, "<< Ono! Profit means positive value. Better luck next time! >>" ) - def test_zero_maxWeight(self): + def test_null_maxWeight(self): """ - Returns ValueError for any zero max_weight value + Returns ValueError for any zero max_Weight value :return: ValueError """ # profit = [10, 20, 30, 40, 50, 60] # weight = [2, 4, 6, 8, 10, 12] - # max_weight = 0 + # max_Weight = null self.assertRaisesRegex( ValueError, - "<< Gotcha! max_weight is a positive quantity greater than zero! >>", + "<< Gotcha! max_Weight is a positive quantity greater than zero! >>", ) def test_unequal_list_length(self): @@ -75,7 +75,7 @@ def test_unequal_list_length(self): """ # profit = [10, 20, 30, 40, 50] # weight = [2, 4, 6, 8, 10, 12] - # max_weight = 100 + # max_Weight = 100 self.assertRaisesRegex( IndexError, "<< The length of both the arrays must be same! Try again.. >>" ) From dbc4085368c320b517ffd3bf160014a057468726 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 9 Jun 2020 16:26:07 +0200 Subject: [PATCH 15/19] Update and rename knapsack_problem.py to knapsack.py --- greedy_method/knapsack.py | 99 +++++++++++++++++++++++++ greedy_method/knapsack_problem.py | 117 ------------------------------ 2 files changed, 99 insertions(+), 117 deletions(-) create mode 100644 greedy_method/knapsack.py delete mode 100644 greedy_method/knapsack_problem.py diff --git a/greedy_method/knapsack.py b/greedy_method/knapsack.py new file mode 100644 index 000000000000..92dd81aaaa82 --- /dev/null +++ b/greedy_method/knapsack.py @@ -0,0 +1,99 @@ +# To get an insight into Greedy Algorithm through the Knapsack problem + + +""" +A shopkeeper has bags of wheat that each have different weights and different profits. +eg. +profit 5 8 7 1 12 3 4 +weight 2 7 1 6 4 2 5 +max_weight 100 + +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. +""" +from typing import Union + + +def calc_profit(profit: list, weight: list, max_weight: int) -> Union[str, int]: + """ + Function description is as follows- + :param profit: Take a list of profits + :param weight: Take a list of weight if bags corresponding to the profits + :param max_weight: Maximum weight that could be carried + :return: Maximum expected gain + + >>> calc_profit([1, 2, 3], [3, 4, 5], 15) + 6 + >>> calc_profit([10, 9 , 8], [3 ,4 , 5], 25) + 27 + """ + if len(profit) != len(weight): + raise ValueError("The length of profit and weight must be same.") + if max_weight <= 0: + raise ValueError("max_weight must greater than zero.") + if any(p < 0 for p in profit): + raise ValueError("Profit can not be negative.") + if any(w < 0 for w in weight): + raise ValueError("Weight can not be negative.") + + # List created to store profit gained for the 1kg in case of each weight + # respectively. Calculate and append profit/weight for each element. + profit_by_weight = [p / w for p, w in zip(profit, weight)] + + # Creating a copy of the list and sorting profit/weight in ascending order + sorted_profit_by_weight = sorted(profit_by_weight) + + # declaring useful variables + length = len(sorted_profit_by_weight) + limit = 0 + gain = 0 + i = 0 + + # loop till the total weight do not reach max limit e.g. 15 kg and till i= weight[index]: + limit += weight[index] + # Adding profit gained for the given weight 1 === + # weight[index]/weight[index] + gain += 1 * profit[index] + else: + # Since the weight encountered is greater than limit, therefore take the + # required number of remaining kgs and calculate profit for it. + # weight remaining / weight[index] + gain += (max_weight - limit) / weight[index] * profit[index] + break + i += 1 + return gain + + +if __name__ == "__main__": + print( + "Input profits, weights, and then max_weight (all positive ints) separated by " + "spaces." + ) + + profit = [int(x) for x in input("Input profits separated by spaces: ").split()] + weight = [int(x) for x in input("Input weights separated by spaces: ").split()] + max_weight = int(input("Max weight allowed: ")) + + # Function Call + calc_profit(profit, weight, max_weight) diff --git a/greedy_method/knapsack_problem.py b/greedy_method/knapsack_problem.py deleted file mode 100644 index 1194f7fd3541..000000000000 --- a/greedy_method/knapsack_problem.py +++ /dev/null @@ -1,117 +0,0 @@ -# To get an insight into Greedy Algorithm -# Knapsack is a common Greedy Algo problem - - -""" -You have bags of wheat with different weights and a different profits for each. -eg. -profit 5 8 7 1 12 3 4 -weight 2 7 1 6 4 2 5 -max_Weight 100 - -Constraints: -max_Weight > 0 -profit[i] >= 0 -weight[i] >= 0 -Calculate the max profit the shopkeeper can make for the given max_Weight that could be carried. -""" -from typing import Union - - -def calc_profit(profit: list, weight: list, max_Weight: int) -> Union[str, int]: - """ - Function description is as follows- - :param profit: Take a list of profits - :param weight: Take a list of weight if bags corresponding to the profits - :param max_Weight: Max. weight that could be carried - :return: Max expected gain - - >>> calc_profit([1,2,3], [3,4,5], 15) - 6 - >>> calc_profit([10, 9 , 8], [3 ,4 , 5], 25) - 27 - """ - if len(profit) != len(weight): - raise IndexError( - "<< The length of both the arrays must be same! Try again.. >>" - ) - - if max_Weight <= 0: - raise ValueError( - "<< Gotcha! max_Weight is a positive quantity greater than zero! >>" - ) - - for i in range(len(profit)): - if profit[i] < 0: - raise ValueError( - "<< Ono! Profit means positive value. Better luck next time! >>" - ) - - if weight[i] < 0: - raise ValueError( - "<< Oops! Could not accept a negative value for weight. Try Again.. >>" - ) - - # List created to store profit gained for the 1kg in case of each weight - # respectively - profit_By_Weight = list() - - # Calculate and append profit/weight for each element - profit_By_Weight = [p / w for p, w in zip(profit, weight)] - - # Creating a copy of the list and sorting profit/weight in ascending order - temp_PBW = sorted(profit_By_Weight) - - # declaring useful variables - length = len(temp_PBW) - limit = 0 - gain = 0 - i = 0 - - # loop till the total weight do not reach max limit e.g. 15 kg and till i= weight[index]: - - limit += weight[index] - # Adding profit gained for the given weight 1 === - # weight[index]/weight[index] - gain += 1 * profit[index] - - else: - # Since the weight encountered is greater than limit, therefore take the required number of - # remaining kgs - # and Calculate profit for it. - # weight remaining/ weight[index] - gain += ((max_Weight - limit) / weight[index]) * profit[index] - break - i += 1 - - return gain - - -if __name__ == "__main__": - - # Input profit, weight and max_Weight (all positive values). - - profit = [int(x) for x in input().split()] - weight = [int(x) for x in input().split()] - max_Weight = int(input("Max weight allowed: ")) - - # Function Call - calc_profit(profit, weight, max_Weight) From 9e91d3bcaa24b0756b0237b93ca082b678c329eb Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 9 Jun 2020 16:33:41 +0200 Subject: [PATCH 16/19] Update test_knapsack.py --- greedy_method/test_knapsack.py | 38 +++++++++++++++++----------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/greedy_method/test_knapsack.py b/greedy_method/test_knapsack.py index 41fb56372467..80efeb09d52d 100644 --- a/greedy_method/test_knapsack.py +++ b/greedy_method/test_knapsack.py @@ -1,33 +1,33 @@ import unittest -import knapsack_problem as kp +import .knapsack as kp class TestClass(unittest.TestCase): """ - Test cases for knapsack_problem + Test cases for knapsack """ def test_sorted(self): """ - kp.calc_profit takes the required argument (profit, weight, max_Weight) + kp.calc_profit takes the required argument (profit, weight, max_weight) and returns whether the answer matches to the expected ones """ profit = [10, 20, 30, 40, 50, 60] weight = [2, 4, 6, 8, 10, 12] - max_Weight = 100 - self.assertEqual(kp.calc_profit(profit, weight, max_Weight), 210) + max_weight = 100 + self.assertEqual(kp.calc_profit(profit, weight, max_weight), 210) - def test_negative_maxWeight(self): + def test_negative_max_weight(self): """ - Returns ValueError for any negative max_Weight value + Returns ValueError for any negative max_weight value :return: ValueError """ # profit = [10, 20, 30, 40, 50, 60] # weight = [2, 4, 6, 8, 10, 12] - # max_Weight = -15 + # max_weight = -15 self.assertRaisesRegex( ValueError, - "<< Gotcha! max_Weight is a positive quantity greater than zero! >>", + "max_weight must greater than zero.", ) def test_negative_profit_Value(self): @@ -37,10 +37,10 @@ def test_negative_profit_Value(self): """ # profit = [10, -20, 30, 40, 50, 60] # weight = [2, 4, 6, 8, 10, 12] - # max_Weight = 15 + # max_weight = 15 self.assertRaisesRegex( ValueError, - "<< Oops! Could not accept a negative value for weight. Try Again.. >>", + "Weight can not be negative.", ) def test_negative_weight_Value(self): @@ -50,22 +50,22 @@ def test_negative_weight_Value(self): """ # profit = [10, 20, 30, 40, 50, 60] # weight = [2, -4, 6, -8, 10, 12] - # max_Weight = 15 + # max_weight = 15 self.assertRaisesRegex( - ValueError, "<< Ono! Profit means positive value. Better luck next time! >>" + ValueError, "Profit can not be negative." ) - def test_null_maxWeight(self): + def test_null_max_weight(self): """ - Returns ValueError for any zero max_Weight value + Returns ValueError for any zero max_weight value :return: ValueError """ # profit = [10, 20, 30, 40, 50, 60] # weight = [2, 4, 6, 8, 10, 12] - # max_Weight = null + # max_weight = null self.assertRaisesRegex( ValueError, - "<< Gotcha! max_Weight is a positive quantity greater than zero! >>", + "max_weight must greater than zero.", ) def test_unequal_list_length(self): @@ -75,9 +75,9 @@ def test_unequal_list_length(self): """ # profit = [10, 20, 30, 40, 50] # weight = [2, 4, 6, 8, 10, 12] - # max_Weight = 100 + # max_weight = 100 self.assertRaisesRegex( - IndexError, "<< The length of both the arrays must be same! Try again.. >>" + IndexError, "The length of profit and weight must be same." ) From 1885d64145b66cdebca0a867f94ff79e202225e8 Mon Sep 17 00:00:00 2001 From: Apoorve73 Date: Tue, 9 Jun 2020 20:49:52 +0530 Subject: [PATCH 17/19] Fix bugs --- greedy_method/test_knapsack.py | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/greedy_method/test_knapsack.py b/greedy_method/test_knapsack.py index 80efeb09d52d..351353908a3a 100644 --- a/greedy_method/test_knapsack.py +++ b/greedy_method/test_knapsack.py @@ -1,5 +1,5 @@ import unittest -import .knapsack as kp +import knapsack as kp class TestClass(unittest.TestCase): @@ -26,8 +26,7 @@ def test_negative_max_weight(self): # weight = [2, 4, 6, 8, 10, 12] # max_weight = -15 self.assertRaisesRegex( - ValueError, - "max_weight must greater than zero.", + ValueError, "max_weight must greater than zero.", ) def test_negative_profit_Value(self): @@ -39,8 +38,7 @@ def test_negative_profit_Value(self): # weight = [2, 4, 6, 8, 10, 12] # max_weight = 15 self.assertRaisesRegex( - ValueError, - "Weight can not be negative.", + ValueError, "Weight can not be negative.", ) def test_negative_weight_Value(self): @@ -51,9 +49,7 @@ def test_negative_weight_Value(self): # profit = [10, 20, 30, 40, 50, 60] # weight = [2, -4, 6, -8, 10, 12] # max_weight = 15 - self.assertRaisesRegex( - ValueError, "Profit can not be negative." - ) + self.assertRaisesRegex(ValueError, "Profit can not be negative.") def test_null_max_weight(self): """ @@ -64,8 +60,7 @@ def test_null_max_weight(self): # weight = [2, 4, 6, 8, 10, 12] # max_weight = null self.assertRaisesRegex( - ValueError, - "max_weight must greater than zero.", + ValueError, "max_weight must greater than zero.", ) def test_unequal_list_length(self): From 7b795c9aa0cf758696768f6850248b1df83eecf5 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 9 Jun 2020 17:34:48 +0200 Subject: [PATCH 18/19] Rename knapsack.py to greedy_knapsack.py --- greedy_method/{knapsack.py => greedy_knapsack.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename greedy_method/{knapsack.py => greedy_knapsack.py} (100%) diff --git a/greedy_method/knapsack.py b/greedy_method/greedy_knapsack.py similarity index 100% rename from greedy_method/knapsack.py rename to greedy_method/greedy_knapsack.py From a062b6eba24fc7731d78db527b06d2947e428884 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 9 Jun 2020 17:36:59 +0200 Subject: [PATCH 19/19] Update test_knapsack.py --- greedy_method/test_knapsack.py | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/greedy_method/test_knapsack.py b/greedy_method/test_knapsack.py index 351353908a3a..8f8107bc7009 100644 --- a/greedy_method/test_knapsack.py +++ b/greedy_method/test_knapsack.py @@ -1,5 +1,5 @@ import unittest -import knapsack as kp +import greedy_knapsack as kp class TestClass(unittest.TestCase): @@ -25,11 +25,9 @@ def test_negative_max_weight(self): # profit = [10, 20, 30, 40, 50, 60] # weight = [2, 4, 6, 8, 10, 12] # max_weight = -15 - self.assertRaisesRegex( - ValueError, "max_weight must greater than zero.", - ) + self.assertRaisesRegex(ValueError, "max_weight must greater than zero.") - def test_negative_profit_Value(self): + def test_negative_profit_value(self): """ Returns ValueError for any negative profit value in the list :return: ValueError @@ -41,7 +39,7 @@ def test_negative_profit_Value(self): ValueError, "Weight can not be negative.", ) - def test_negative_weight_Value(self): + def test_negative_weight_value(self): """ Returns ValueError for any negative weight value in the list :return: ValueError @@ -59,9 +57,7 @@ def test_null_max_weight(self): # profit = [10, 20, 30, 40, 50, 60] # weight = [2, 4, 6, 8, 10, 12] # max_weight = null - self.assertRaisesRegex( - ValueError, "max_weight must greater than zero.", - ) + self.assertRaisesRegex(ValueError, "max_weight must greater than zero.") def test_unequal_list_length(self): """ @@ -71,9 +67,7 @@ def test_unequal_list_length(self): # profit = [10, 20, 30, 40, 50] # weight = [2, 4, 6, 8, 10, 12] # max_weight = 100 - self.assertRaisesRegex( - IndexError, "The length of profit and weight must be same." - ) + self.assertRaisesRegex(IndexError, "The length of profit and weight must be same.") if __name__ == "__main__":