@@ -8,7 +8,16 @@ class Item:
8
8
weight : int
9
9
value : int
10
10
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
+ """
12
21
return self .weight == other .weight and self .value == other .value
13
22
14
23
@@ -54,18 +63,6 @@ def fractional_cover(items: list[Item], capacity: int) -> float:
54
63
>>> fractional_cover(items, 1)
55
64
6.0
56
65
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
-
69
66
>>> items = [Item(1, 1)]
70
67
>>> fractional_cover(items, 0)
71
68
0.0
@@ -74,7 +71,7 @@ def fractional_cover(items: list[Item], capacity: int) -> float:
74
71
ratios = [(item .value / item .weight , item ) for item in items ]
75
72
76
73
# 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 )
78
75
79
76
total_value = 0.0
80
77
remaining_capacity = capacity
@@ -89,7 +86,6 @@ def fractional_cover(items: list[Item], capacity: int) -> float:
89
86
90
87
return total_value
91
88
92
-
93
89
if __name__ == "__main__" :
94
90
import doctest
95
91
0 commit comments