MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / greedy

Function greedy

other/greedy.py:30–39  ·  view source on GitHub ↗
(item, max_cost, key_func)

Source from the content-addressed store, hash-verified

28
29
30def greedy(item, max_cost, key_func):
31 items_copy = sorted(item, key=key_func, reverse=True)
32 result = []
33 total_value, total_cost = 0.0, 0.0
34 for i in range(len(items_copy)):
35 if (total_cost + items_copy[i].get_weight()) <= max_cost:
36 result.append(items_copy[i])
37 total_cost += items_copy[i].get_weight()
38 total_value += items_copy[i].get_value()
39 return (result, total_value)
40
41
42def test_greedy():

Callers

nothing calls this directly

Calls 3

get_weightMethod · 0.80
appendMethod · 0.45
get_valueMethod · 0.45

Tested by

no test coverage detected