From 2201f88fe52fe5f4cb3adf92edca3ec41de9d89f Mon Sep 17 00:00:00 2001 From: algobytewise Date: Wed, 7 Apr 2021 15:33:34 +0530 Subject: [PATCH 1/3] fixes --- dynamic_programming/fractional_knapsack_2.py | 14 +++++++------- dynamic_programming/knapsack.py | 2 +- .../longest_increasing_subsequence.py | 2 +- mypy.ini | 2 +- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/dynamic_programming/fractional_knapsack_2.py b/dynamic_programming/fractional_knapsack_2.py index cae57738311b..dd497e8b4334 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[float, list[float]]: """ >>> value = [1, 3, 5, 7, 9] >>> weight = [0.9, 0.7, 0.5, 0.3, 0.1] @@ -32,8 +32,8 @@ def fractional_knapsack( ratio = [v / w for v, w in zip(value, weight)] index.sort(key=lambda i: ratio[i], reverse=True) - max_value = 0 - fractions = [0] * len(value) + max_value: float = 0 + fractions: list[float] = [0] * len(value) for i in index: if weight[i] <= capacity: fractions[i] = 1 @@ -49,10 +49,10 @@ 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] + valueStrings = input(f"Enter the values of the {n} item(s) in order: ").split() + value = [int(v) for v in valueStrings] + weightStrings = input(f"Enter the positive weights of the {n} item(s) in order: ").split() + weight = [int(w) for w in weightStrings] 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 diff --git a/mypy.ini b/mypy.ini index 9eec22e22717..c8a8fab4bfeb 100644 --- a/mypy.ini +++ b/mypy.ini @@ -2,4 +2,4 @@ ignore_missing_imports = True ; FIXME: #4052 fix mypy errors in the exclude directories and remove them below -exclude = (data_structures|dynamic_programming|graphs|maths|matrix|other|project_euler|searches|strings*)/$ +exclude = (data_structures|graphs|maths|matrix|other|project_euler|searches|strings*)/$ From b40ae7234ab25985e8428c0594e69de5c2913beb Mon Sep 17 00:00:00 2001 From: algobytewise Date: Wed, 7 Apr 2021 15:53:11 +0530 Subject: [PATCH 2/3] fix formatting --- dynamic_programming/fractional_knapsack_2.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/dynamic_programming/fractional_knapsack_2.py b/dynamic_programming/fractional_knapsack_2.py index dd497e8b4334..495f122b0267 100644 --- a/dynamic_programming/fractional_knapsack_2.py +++ b/dynamic_programming/fractional_knapsack_2.py @@ -51,7 +51,9 @@ def fractional_knapsack( n = int(input("Enter number of items: ")) valueStrings = input(f"Enter the values of the {n} item(s) in order: ").split() value = [int(v) for v in valueStrings] - weightStrings = input(f"Enter the positive weights of the {n} item(s) in order: ").split() + weightStrings = input( + f"Enter the positive weights of the {n} item(s) in order: " + ).split() weight = [int(w) for w in weightStrings] capacity = int(input("Enter maximum weight: ")) From 93bbd7ee04079e1f4ed43f0851638d3511a05404 Mon Sep 17 00:00:00 2001 From: algobytewise Date: Thu, 8 Apr 2021 14:38:03 +0530 Subject: [PATCH 3/3] snake_case & restore mypy.ini --- dynamic_programming/fractional_knapsack_2.py | 8 ++++---- mypy.ini | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/dynamic_programming/fractional_knapsack_2.py b/dynamic_programming/fractional_knapsack_2.py index 495f122b0267..073a8980fca6 100644 --- a/dynamic_programming/fractional_knapsack_2.py +++ b/dynamic_programming/fractional_knapsack_2.py @@ -49,12 +49,12 @@ def fractional_knapsack( if __name__ == "__main__": n = int(input("Enter number of items: ")) - valueStrings = input(f"Enter the values of the {n} item(s) in order: ").split() - value = [int(v) for v in valueStrings] - weightStrings = input( + value_strings = input(f"Enter the values of the {n} item(s) in order: ").split() + value = [int(v) for v in value_strings] + weight_strings = input( f"Enter the positive weights of the {n} item(s) in order: " ).split() - weight = [int(w) for w in weightStrings] + weight = [int(w) for w in weight_strings] capacity = int(input("Enter maximum weight: ")) max_value, fractions = fractional_knapsack(value, weight, capacity) diff --git a/mypy.ini b/mypy.ini index c8a8fab4bfeb..9eec22e22717 100644 --- a/mypy.ini +++ b/mypy.ini @@ -2,4 +2,4 @@ ignore_missing_imports = True ; FIXME: #4052 fix mypy errors in the exclude directories and remove them below -exclude = (data_structures|graphs|maths|matrix|other|project_euler|searches|strings*)/$ +exclude = (data_structures|dynamic_programming|graphs|maths|matrix|other|project_euler|searches|strings*)/$