Skip to content

There is an invalid case also in Knapsack. #4911

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

Closed
wants to merge 1 commit into from
Closed

There is an invalid case also in Knapsack. #4911

wants to merge 1 commit into from

Conversation

abhijitgawai
Copy link

Many people ignore it. Please add this invalid case where input is worng (when length of weight and value is not equal)

Describe your change:

There is invalid case in knapsack. Many people ignore it. I have included that case in knapsack.py
Invalid case is a case where weight and value array is not of equal length

Therefore I added - if len(w)!=len(val): return -1

I have returned -1 because otherwise user will not get to know that input was wrong.
If I would have returned 0, user would think, there is no space in bag to accept least weight.

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

…ase add this invalid case where input is worng (when length of weight and value is not equal)
@ghost ghost added awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files labels Oct 2, 2021
@@ -20,6 +20,10 @@ def knapsack(capacity: int, weights: list[int], values: list[int], counter: int)
which is the limit of the capacity.
"""

# Invalid Case
if len(val)!=len(w):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please recheck the variable name.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do i do that now. Can you please suggest me. Should i create new PR or what. Really new here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as you can see if knapsack called like knapsack(cap, w, val, c) then knapsack function accept the variables to named capacity, weights, values and counter.
so, there is no val and w now but only use values and weights.
then now you can code like this -> if len(values) != len(weights)

@abhijitgawai
Copy link
Author

Hey. I previously didn't knew about branching. So, this time I created a new branch for pull request here #4956

Please apologize for any inconvenience

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants