From fa789fc0ebb78b257c0cfb931956b6f544fa7e84 Mon Sep 17 00:00:00 2001 From: joshijai2 Date: Tue, 13 Apr 2021 18:51:26 +0530 Subject: [PATCH 1/2] [mypy] Fix type annotations for dynamic programming algorithms * fixed "Need type annotation" in knapsack.py, longest_increasing_subsequence.py * fixed return type error for function. * fixed a bug and type error in input. --- dynamic_programming/fractional_knapsack_2.py | 24 ++++++++++++------- dynamic_programming/knapsack.py | 2 +- .../longest_increasing_subsequence.py | 2 +- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/dynamic_programming/fractional_knapsack_2.py b/dynamic_programming/fractional_knapsack_2.py index cae57738311b..7b49edf13da3 100644 --- a/dynamic_programming/fractional_knapsack_2.py +++ b/dynamic_programming/fractional_knapsack_2.py @@ -7,7 +7,7 @@ def fractional_knapsack( value: list[int], weight: list[int], capacity: int -) -> tuple[int, list[int]]: +) -> tuple[int, list[float]]: """ >>> value = [1, 3, 5, 7, 9] >>> weight = [0.9, 0.7, 0.5, 0.3, 0.1] @@ -33,15 +33,15 @@ def fractional_knapsack( index.sort(key=lambda i: ratio[i], reverse=True) max_value = 0 - fractions = [0] * len(value) + fractions = [0.0] * len(value) for i in index: if weight[i] <= capacity: fractions[i] = 1 max_value += value[i] capacity -= weight[i] else: - fractions[i] = capacity / weight[i] - max_value += value[i] * capacity / weight[i] + fractions[i] = round(capacity / weight[i], 2) + max_value += round(value[i] * capacity / weight[i]) break return max_value, fractions @@ -49,10 +49,18 @@ def fractional_knapsack( if __name__ == "__main__": n = int(input("Enter number of items: ")) - value = input(f"Enter the values of the {n} item(s) in order: ").split() - value = [int(v) for v in value] - weight = input(f"Enter the positive weights of the {n} item(s) in order: ".split()) - weight = [int(w) for w in weight] + value = [ + int(v) + for v in input(f"Enter the values of the {n} item(s) in order: ") + .strip() + .split() + ] + weight = [ + int(w) + for w in input(f"Enter the positive weights of the {n} item(s) in order: ") + .strip() + .split() + ] capacity = int(input("Enter maximum weight: ")) max_value, fractions = fractional_knapsack(value, weight, capacity) diff --git a/dynamic_programming/knapsack.py b/dynamic_programming/knapsack.py index 69e54c00aa4e..01f3cd25e7b6 100644 --- a/dynamic_programming/knapsack.py +++ b/dynamic_programming/knapsack.py @@ -91,7 +91,7 @@ def knapsack_with_example_solution(W: int, wt: list, val: list): ) optimal_val, dp_table = knapsack(W, wt, val, num_items) - example_optional_set = set() + example_optional_set: set[int] = set() _construct_solution(dp_table, wt, num_items, W, example_optional_set) return optimal_val, example_optional_set diff --git a/dynamic_programming/longest_increasing_subsequence.py b/dynamic_programming/longest_increasing_subsequence.py index f5ca8a2b5cdc..a029f9be7d98 100644 --- a/dynamic_programming/longest_increasing_subsequence.py +++ b/dynamic_programming/longest_increasing_subsequence.py @@ -36,7 +36,7 @@ def longest_subsequence(array: list[int]) -> list[int]: # This function is recu pivot = array[0] isFound = False i = 1 - longest_subseq = [] + longest_subseq: list[int] = [] while not isFound and i < array_length: if array[i] < pivot: isFound = True From e50d943154411c982c8abe72c8aa79fd138f7be2 Mon Sep 17 00:00:00 2001 From: joshijai2 Date: Tue, 13 Apr 2021 20:02:02 +0530 Subject: [PATCH 2/2] Fix bug in docstring Changed data type of a testcase as per the return type mentioned. --- dynamic_programming/fractional_knapsack_2.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dynamic_programming/fractional_knapsack_2.py b/dynamic_programming/fractional_knapsack_2.py index 7b49edf13da3..9c8ed14f1695 100644 --- a/dynamic_programming/fractional_knapsack_2.py +++ b/dynamic_programming/fractional_knapsack_2.py @@ -20,7 +20,7 @@ def fractional_knapsack( >>> fractional_knapsack(value, weight, 26) (25, [1, 1, 1, 1, 1]) >>> fractional_knapsack(value, weight, -1) - (-90.0, [0, 0, 0, 0, -10.0]) + (-90, [0.0, 0.0, 0.0, 0.0, -10.0]) >>> fractional_knapsack([1, 3, 5, 7], weight, 30) (16, [1, 1, 1, 1]) >>> fractional_knapsack(value, [0.9, 0.7, 0.5, 0.3, 0.1], 30)