mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-04-20 04:37:36 +00:00
[mypy] Fix type annotations for dynamic programming (#4687)
* Fix mypy error for knapsack.py * Fix mypy error for longest_increasing_subsequence * Fix mypy error for fractional_knapsack_2.py
This commit is contained in:
parent
757d4fb84f
commit
c1b15a86ba
@ -7,7 +7,7 @@ from __future__ import annotations
|
|||||||
|
|
||||||
def fractional_knapsack(
|
def fractional_knapsack(
|
||||||
value: list[int], weight: list[int], capacity: int
|
value: list[int], weight: list[int], capacity: int
|
||||||
) -> tuple[int, list[int]]:
|
) -> tuple[float, list[float]]:
|
||||||
"""
|
"""
|
||||||
>>> value = [1, 3, 5, 7, 9]
|
>>> value = [1, 3, 5, 7, 9]
|
||||||
>>> weight = [0.9, 0.7, 0.5, 0.3, 0.1]
|
>>> 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)]
|
ratio = [v / w for v, w in zip(value, weight)]
|
||||||
index.sort(key=lambda i: ratio[i], reverse=True)
|
index.sort(key=lambda i: ratio[i], reverse=True)
|
||||||
|
|
||||||
max_value = 0
|
max_value: float = 0
|
||||||
fractions = [0] * len(value)
|
fractions: list[float] = [0] * len(value)
|
||||||
for i in index:
|
for i in index:
|
||||||
if weight[i] <= capacity:
|
if weight[i] <= capacity:
|
||||||
fractions[i] = 1
|
fractions[i] = 1
|
||||||
@ -48,13 +48,6 @@ def fractional_knapsack(
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
n = int(input("Enter number of items: "))
|
import doctest
|
||||||
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]
|
|
||||||
capacity = int(input("Enter maximum weight: "))
|
|
||||||
|
|
||||||
max_value, fractions = fractional_knapsack(value, weight, capacity)
|
doctest.testmod()
|
||||||
print("The maximum value of items that can be carried:", max_value)
|
|
||||||
print("The fractions in which the items should be taken:", fractions)
|
|
||||||
|
@ -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)
|
optimal_val, dp_table = knapsack(W, wt, val, num_items)
|
||||||
example_optional_set = set()
|
example_optional_set: set = set()
|
||||||
_construct_solution(dp_table, wt, num_items, W, example_optional_set)
|
_construct_solution(dp_table, wt, num_items, W, example_optional_set)
|
||||||
|
|
||||||
return optimal_val, example_optional_set
|
return optimal_val, example_optional_set
|
||||||
|
@ -36,7 +36,7 @@ def longest_subsequence(array: list[int]) -> list[int]: # This function is recu
|
|||||||
pivot = array[0]
|
pivot = array[0]
|
||||||
isFound = False
|
isFound = False
|
||||||
i = 1
|
i = 1
|
||||||
longest_subseq = []
|
longest_subseq: list[int] = []
|
||||||
while not isFound and i < array_length:
|
while not isFound and i < array_length:
|
||||||
if array[i] < pivot:
|
if array[i] < pivot:
|
||||||
isFound = True
|
isFound = True
|
||||||
|
Loading…
x
Reference in New Issue
Block a user