-
Notifications
You must be signed in to change notification settings - Fork 0
/
pc.py
51 lines (32 loc) · 1.15 KB
/
pc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import random
class Thing:
def __init__(self, weight: float, value: float) -> None:
self.weight = weight
self.value = value
def fits(capacity: int, objects: list[Thing], threshold: int) -> bool:
# sum of objects weight <= capacity
# sum of objects value >= threshold
remain = capacity
result = 0
i = 0
stopProc = False
while (stopProc != True):
if (objects[i].weight <= remain):
remain -= objects[i].weight
result += objects[i].value
print("Pack ", i, " - Weight ", objects[i].weight, " - Value ", objects[i].value)
if (objects[i].weight > remain):
i += 1
if (i == threshold):
# if result > threshold or i == len(objects):
stopProc = True
print("Max Value:\t", result)
return result >= threshold
if __name__ == "__main__":
capacity = 100
threshold = 50
things: list[Thing] = []
for i in range(100):
things.append(Thing(random.randint(1, 10), i))
print("backpack {", "capacity", "=>", capacity, "}")#, "things", "=>", things, "}")
print("fits", "=>", fits(capacity, things, threshold))