-
-
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 7 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,104 @@ | ||
# 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. | ||
""" | ||
|
||
# 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 | ||
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]) | ||
|
||
# Creating a copy of the list and sorting proit/weight in ascending order | ||
temp_PBW = sorted(copy.deepcopy(profit_By_Weight)) | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# declaring useful variables | ||
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<l | ||
while limit <= 15 and i < length: | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# 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 15 - 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 | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# 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()) | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if len(profit) != len(weight): | ||
print("The length of both the arrays must be same! Try again!") | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if max_Weight < 0: | ||
print("Gotcha! Weight is a positive quantity") | ||
|
||
for i in range(len(profit)): | ||
if (profit[i] or weight[i]) < 0: | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
print("Ono! Input positive values only! Better luck next time") | ||
break | ||
else: | ||
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,21 @@ | ||
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 | ||
""" | ||
gain = kp.calc_Profit([10, 20, 30, 40, 50, 60], [2, 4, 6, 8, 10, 12], 100) | ||
# self.assertTrue(result) | ||
self.assertEqual(gain, 75) | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
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.