I have an optimization problem of trying to use less variety of products.
For example:
screws = [6,8,16,18,39]
I would like to exchange those 5 screws with 3. So I need to use the strongest so I could select, [8,18,39]
. Using any other option will cause a waste—for example, screw 16 is too strong for location of 6, so [16,18,39]
is not as good. I would like to write an algorithm that would be useful for a larger number of parts also. So far I tried this:
def split_list(data, n):
from itertools import combinations, chain
for splits in combinations(range(1, len(data)), n-1):
result = []
prev = None
for split in chain(splits, [None]):
result.append(data[prev:split])
prev = split
yield result
new_list = list(split_list(screws, 3))
#print list(split_list(screws, 3))
As a result of running this code I got a list of lists:
[[[6], [8], [16, 18, 39]],
[[6], [8, 16], [18, 39]],
[[6], [8, 16, 18], [39]],
[[6, 8], [16], [18, 39]],
[[6, 8], [16, 18], [39]],
[[6, 8, 16], [18], [39]]]
I would like to find out local maximums from all the list. For example in the first list [[6], [8],[16, 18, 39]]
, maximum = 6, maximum = 8, maximum = 39, and so on. But I don't know how to. Is there a way to find local maximum of all nested lists? I'm stuck in this moment, could you help me? I would appreciate also help with further progress.
Later on, I would like to check the sum of differences between maximum and other elements in the same list. So, 6-6 = 0, 8-8 = 0, and last 39-16+30-18-39-39 = 35. This will allow me to find out the smallest value from all lists of lists. This will be the most optimal solution. So the final result should be [[6, 8], [16, 18], [39]]
and from it I would select [8,18,39]
.
This is basically my first program after tutorials and online classes, so all help is very welcome.