From 7797a907f1dffe1803397bb9a4da75f887ea5046 Mon Sep 17 00:00:00 2001 From: abhijitgawai Date: Sat, 2 Oct 2021 20:13:09 +0530 Subject: [PATCH] There is an invalid case also in Knapsack. Many people ignore it. Please add this invalid case where input is worng (when length of weight and value is not equal) --- knapsack/knapsack.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/knapsack/knapsack.py b/knapsack/knapsack.py index 18a36c3bcdda..9639c90c1909 100644 --- a/knapsack/knapsack.py +++ b/knapsack/knapsack.py @@ -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): + return -1 + # Base Case if counter == 0 or capacity == 0: return 0