combinations of sizes for shipping

Posted by Eadz on Stack Overflow See other posts from Stack Overflow or by Eadz
Published on 2010-04-13T06:22:01Z Indexed on 2010/04/13 6:22 UTC
Read the original article Hit count: 377

Hi there, I've got a bunch of products with sizes to ship and I need to find out the cheapest rate.

Given a shipment made out of sizes, say [1,3,3,5] I need to decide how to ship - all together or separate. However it's not as simple as [1,3,3,5] or 1 & 3 & 3 & 5, i need all of the possible combinations something like:

[
[[1,3,3,5]],           ( 1 shipment )
[[1],[3,3,5]],         ( 2 shipments )
[[1,3],[3,5]],         ( 2 shipments )
[[1,3,3],[5]],         ( 2 shipments )
[[1,5],[3,3]],         ( 2 shipments ) 
[[1,3],[3],[5]],       ( 3 shipments )
[[1],[3],[3],[5]]      ( 4 shipments )
]

( etc - many more i assume ) I've tried combinations from the facets gem but it's not quite what i'm after, and I'm not sure how else to approach this problem. I understand it probably has a name and a solution if only I knew the name :)

I understand there could be a lot of combinations, but the initial array of sizes won't be larger than 7 or so.

Thanks in advance!

© Stack Overflow or respective owner

Related posts about ruby

Related posts about multidimensional-array