-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Create-Add files to greedy_method directory #2082
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 9 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
91e9626
Add Greedy Method Approach
Apoorve73 a3b265f
Update Filename
Apoorve73 7205ad5
Update Variable and Links
Apoorve73 746d19f
Fixed flake8 bugs
Apoorve73 d4c652d
Update unittest filename
Apoorve73 33318b4
Update unittest filename
Apoorve73 8dc157d
Final unittest filename update
Apoorve73 8a112ae
Pythonic Code formatting
Apoorve73 6be08f5
flake8 fixes
Apoorve73 38652c0
lowercase function name
Apoorve73 0d0b2fc
Add zip function
Apoorve73 6033533
Add zip function
Apoorve73 151e2fd
params lowercase
Apoorve73 5b862d9
Travis CI fixes
Apoorve73 dbc4085
Update and rename knapsack_problem.py to knapsack.py
cclauss 9e91d3b
Update test_knapsack.py
cclauss 1885d64
Fix bugs
Apoorve73 7b795c9
Rename knapsack.py to greedy_knapsack.py
cclauss a062b6e
Update test_knapsack.py
cclauss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
# 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 | ||
for i in range(len(weight)): | ||
profit_By_Weight.append(profit[i] / weight[i]) | ||
Apoorve73 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# 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<length | ||
while limit <= max_Weight and i < length: | ||
|
||
# flag value for encountered greatest element in temp_PBW | ||
flag = temp_PBW[length - i - 1] | ||
""" | ||
Calculating index of the same flag in profit_By_Weight list. | ||
This will give the index of the first encountered element which is same as of flag. | ||
There may be one or more values same as that of flag but .index always encounter the | ||
very first element only. | ||
To curb this alter the values in profit_By_Weight once they are used Here it is done to -1 | ||
because neither profit nor weight can be in negative. | ||
""" | ||
index = profit_By_Weight.index(flag) | ||
profit_By_Weight[index] = -1 | ||
|
||
# check if the weight encountered is less than the total weight | ||
# encountered before. | ||
if max_Weight - limit >= 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import unittest | ||
import knapsack_problem as kp | ||
|
||
|
||
class TestClass(unittest.TestCase): | ||
""" | ||
Test cases for knapsack_problem | ||
""" | ||
|
||
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 | ||
""" | ||
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__": | ||
unittest.main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.