Skip to content

Commit 18b625f

Browse files
authored
Update fractional_cover_problem.py
1 parent 9111d75 commit 18b625f

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

greedy_methods/fractional_cover_problem.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,16 @@ class Item:
88
weight: int
99
value: int
1010

11-
def __eq__(self, other):
11+
def __eq__(self, other: 'Item') -> bool:
12+
"""
13+
Compare two Item objects for equality based on weight and value attributes.
14+
15+
Args:
16+
other (Item): Another Item object to compare with.
17+
18+
Returns:
19+
bool: True if the objects are equal, False otherwise.
20+
"""
1221
return self.weight == other.weight and self.value == other.value
1322

1423

@@ -54,18 +63,6 @@ def fractional_cover(items: list[Item], capacity: int) -> float:
5463
>>> fractional_cover(items, 1)
5564
6.0
5665
57-
>>> items = [Item(1, 1)]
58-
>>> fractional_cover(items, 0)
59-
0.0
60-
61-
>>> items = [Item(1, 1)]
62-
>>> fractional_cover(items, 1)
63-
1.0
64-
65-
>>> items = [Item(1, 1)]
66-
>>> fractional_cover(items, 2)
67-
1.0
68-
6966
>>> items = [Item(1, 1)]
7067
>>> fractional_cover(items, 0)
7168
0.0
@@ -74,7 +71,7 @@ def fractional_cover(items: list[Item], capacity: int) -> float:
7471
ratios = [(item.value / item.weight, item) for item in items]
7572

7673
# Sort the items by their value-to-weight ratio in descending order
77-
ratios.sort(key=lambda x: x[0], reverse=True)
74+
ratios.sort(key=lambda item_ratio: item_ratio[0], reverse=True)
7875

7976
total_value = 0.0
8077
remaining_capacity = capacity
@@ -89,7 +86,6 @@ def fractional_cover(items: list[Item], capacity: int) -> float:
8986

9087
return total_value
9188

92-
9389
if __name__ == "__main__":
9490
import doctest
9591

0 commit comments

Comments
 (0)