-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlgorithmeGourmand.py
64 lines (46 loc) · 2.12 KB
/
AlgorithmeGourmand.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
52
53
54
55
56
57
58
59
60
61
62
63
64
class AlgorithmeGourmand(object):
def __init__(self, n, v, w):
self.name = n
self.value = v
self.calories = w
def getValue(self):
return self.value
def getCost(self):
return self.calories
def density(self):
return self.getValue() / self.getCost()
def __str__(self):
return self.name + ': <' + str(self.value) + ', ' + str(self.calories) + '>'
def buildMenu(names, values, calories):
menu = []
for i in range(len(values)):
menu.append(AlgorithmeGourmand(names[i], values[i], calories[i]))
return menu
def AlgorithmeFlexible(items, maxCost, keyFunction):
itemsCopy = sorted(items, key=keyFunction, reverse=True)
result = []
totalValue, totalCost = 0.0, 0.0
for i in range(len(itemsCopy)):
if (totalCost + itemsCopy[i].getCost()) <= maxCost:
result.append(itemsCopy[i])
totalCost += itemsCopy[i].getCost()
totalValue += itemsCopy[i].getValue()
return result, totalValue
def testAlgorithmeFlexible(items, constraint, keyFunction):
taken, val = AlgorithmeFlexible(items, constraint, keyFunction)
print("Total value of items taken = ", val)
for item in taken:
print(" ", item)
def testAlgorithmeFlexibles(foods, maxUnits):
print("Use AlgorithmeFlexible by value to allocate", maxUnits, "calories")
testAlgorithmeFlexible(foods, maxUnits, AlgorithmeGourmand.getValue)
print("\nUse AlgorithmeFlexible by cost to allocate", maxUnits, "calories")
testAlgorithmeFlexible(foods, maxUnits, lambda x: 1 / AlgorithmeGourmand.getCost(x))
print("\nUse AlgorithmeFlexible by density to allocate", maxUnits, "calories")
testAlgorithmeFlexible(foods, maxUnits, AlgorithmeGourmand.density)
names = ["wine", "beer", "pizza", "burger", "fries", "cola", "apple", "donut", "cake"]
values = [80, 90, 95, 100, 90, 79, 50, 10]
calories = [123, 154, 258, 354, 365, 150, 95, 195]
foods = buildMenu(names, values, calories)
testAlgorithmeFlexibles(foods, 750)
# https://github.com/dadou-steven/mathematical/blob/main/src/main/java/appsinc/fr/lesmaths/AlgorithmeGourmand.java