Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions dynamic_programming/fractional_knapsack_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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)
Expand All @@ -33,26 +33,34 @@ 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


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)
Expand Down
2 changes: 1 addition & 1 deletion dynamic_programming/knapsack.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion dynamic_programming/longest_increasing_subsequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down