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

Function calc_profit

knapsack/greedy_knapsack.py:20–84  ·  view source on GitHub ↗

Function description is as follows- :param profit: Take a list of profits :param weight: Take a list of weight if bags corresponding to the profits :param max_weight: Maximum weight that could be carried :return: Maximum expected gain >>> calc_profit([1, 2, 3], [3, 4, 5], 1

(profit: list, weight: list, max_weight: int)

Source from the content-addressed store, hash-verified

18
19
20def calc_profit(profit: list, weight: list, max_weight: int) -> int:
21 """
22 Function description is as follows-
23 :param profit: Take a list of profits
24 :param weight: Take a list of weight if bags corresponding to the profits
25 :param max_weight: Maximum weight that could be carried
26 :return: Maximum expected gain
27
28 >>> calc_profit([1, 2, 3], [3, 4, 5], 15)
29 6
30 >>> calc_profit([10, 9 , 8], [3 ,4 , 5], 25)
31 27
32 """
33 if len(profit) != len(weight):
34 raise ValueError("The length of profit and weight must be same.")
35 if max_weight <= 0:
36 raise ValueError("max_weight must greater than zero.")
37 if any(p < 0 for p in profit):
38 raise ValueError("Profit can not be negative.")
39 if any(w < 0 for w in weight):
40 raise ValueError("Weight can not be negative.")
41
42 # List created to store profit gained for the 1kg in case of each weight
43 # respectively. Calculate and append profit/weight for each element.
44 profit_by_weight = [p / w for p, w in zip(profit, weight)]
45
46 # Creating a copy of the list and sorting profit/weight in ascending order
47 sorted_profit_by_weight = sorted(profit_by_weight)
48
49 # declaring useful variables
50 length = len(sorted_profit_by_weight)
51 limit = 0
52 gain = 0
53 i = 0
54
55 # loop till the total weight do not reach max limit e.g. 15 kg and till i<length
56 while limit <= max_weight and i < length:
57 # flag value for encountered greatest element in sorted_profit_by_weight
58 biggest_profit_by_weight = sorted_profit_by_weight[length - i - 1]
59 """
60 Calculate the index of the biggest_profit_by_weight in profit_by_weight list.
61 This will give the index of the first encountered element which is same as of
62 biggest_profit_by_weight. There may be one or more values same as that of
63 biggest_profit_by_weight but index always encounter the very first element
64 only. To curb this alter the values in profit_by_weight once they are used
65 here it is done to -1 because neither profit nor weight can be in negative.
66 """
67 index = profit_by_weight.index(biggest_profit_by_weight)
68 profit_by_weight[index] = -1
69
70 # check if the weight encountered is less than the total weight
71 # encountered before.
72 if max_weight - limit >= weight[index]:
73 limit += weight[index]
74 # Adding profit gained for the given weight 1 ===
75 # weight[index]/weight[index]
76 gain += 1 * profit[index]
77 else:

Callers 1

greedy_knapsack.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected