Skip to content

Instantly share code, notes, and snippets.

@riccamastellone
Created June 29, 2017 10:27
Show Gist options
  • Save riccamastellone/53458673ec62f52537f11930a5bd615b to your computer and use it in GitHub Desktop.
Save riccamastellone/53458673ec62f52537f11930a5bd615b to your computer and use it in GitHub Desktop.
We want to maximise profit selling metal rods: we can cut them to sell more, but performing cuts, has a cost
# We want to maximise profit selling metal rods: we can cut them to sell more, but performing cuts, has a cost
#
# If we sell metal rods of length, he receives N x L x metal_price.
# The remaining smaller metal rods will be thrown away.
# To cut the metal rods, we needs to pay cost_per_cut for every cut.
def get_max_profit(cost_per_cut, price, rods):
longest_rod = max(rods)
max_profit = 0
for size in range(1, longest_rod):
profit = 0
for i in range(0, len(rods)):
if size > rods[i]:
continue
# Current rod price after cutting it
curr_price = (rods[i] / size ) * price * size
# Number of cuts
cuts = rods[i] / size - 1 if rods[i] % size == 0 else rods[i] / size
# Current profit
curr_profit = curr_price - cost_per_cut * cuts
if curr_profit > 0:
profit += curr_profit
if profit > max_profit:
max_profit = profit
return max_profit
# Example
print get_max_profit(100, 10, [26, 103, 59]) # 1230
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment