Skip to content

Added Fractional Knapsack Problem #11770

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
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
36 changes: 36 additions & 0 deletions knapsack/fractional_knapsack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
def fractional_knapsack(capacity, values, weights):

"""
>>> capacity = 50
>>> values = [60, 100, 120]
>>> weight = [10, 20, 30]
>>> fractional_knapsack(capacity, values, weight)
240.0
"""

Check failure on line 9 in knapsack/fractional_knapsack.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

knapsack/fractional_knapsack.py:9:8: W291 Trailing whitespace
# Calculate value-to-weight ratio for each item

Check failure on line 11 in knapsack/fractional_knapsack.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

knapsack/fractional_knapsack.py:11:1: W293 Blank line contains whitespace
items = [(values[i] / weights[i], values[i], weights[i]) for i in range(len(values))]

Check failure on line 12 in knapsack/fractional_knapsack.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

knapsack/fractional_knapsack.py:12:89: E501 Line too long (89 > 88)

Check failure on line 13 in knapsack/fractional_knapsack.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

knapsack/fractional_knapsack.py:13:1: W293 Blank line contains whitespace
# Sort items by their value-to-weight ratio in descending order
items.sort(key=lambda x: x[0], reverse=True)

Check failure on line 16 in knapsack/fractional_knapsack.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

knapsack/fractional_knapsack.py:16:1: W293 Blank line contains whitespace
total_value = 0 # Total value of items taken

Check failure on line 18 in knapsack/fractional_knapsack.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

knapsack/fractional_knapsack.py:18:1: W293 Blank line contains whitespace
for ratio, value, weight in items:
if capacity == 0: # If the knapsack is full, break the loop
break

Check failure on line 22 in knapsack/fractional_knapsack.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

knapsack/fractional_knapsack.py:22:1: W293 Blank line contains whitespace
if weight <= capacity:
# Take the whole item
total_value += value
capacity -= weight
else:
total_value += ratio * capacity # Value of the fraction taken
capacity = 0 # The knapsack is now full

Check failure on line 30 in knapsack/fractional_knapsack.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

knapsack/fractional_knapsack.py:30:1: W293 Blank line contains whitespace
return total_value


if __name__ == "__main__":
import doctest
doctest.testmod()

Check failure on line 36 in knapsack/fractional_knapsack.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W292)

knapsack/fractional_knapsack.py:36:22: W292 No newline at end of file
Loading