Sum list numbers and compare to another value?

prof_lofi's icon

Hi all,

Apologies if I'm missing previous posts concerning this but I haven't found an obvious solution and it's doing my head in (I'm not great at Max), even explaining is seems difficult.

I'm trying to make a simple patch that contains a list of numbers whose sum can be filtered by another number...sort of. The patch is to contain a list of values low to high representing different duration in seconds. I'd like to be able to enter another number representing the total duration of a performance that could contain all of the values in the list whose sum is

For example, if the list contains these values for seconds [1 2 3 4 5 6 7 8 9 10] and I enter 40 seconds as the performance's total duration, I'd like the patch to output: [1 2 3 4 5 6 7 8] whose sum is 36 and not to include 9 or 10 whose sums would be 45 or 55 and thus too long etc.

I've looked into expre, vexpr, all of the zl objects, and posts here involving expressions, but I think I'm getting to the point of reinventing the wheel!

Attached is my experimental patch...to the left is where I'm trying to figure out how to compare the sum to the total performance and then to exclude the largest member and repeat the process etc until the result is achieved. Apologies for the messy patching and of if this is too big of a problem to sort on the forum.

Cheers,

Bill

prof_lofi's icon

damn...forgot to attach the patch...

Bill

4202.convertsecondstominutesandseconds.maxpat
Max Patch
Holland Hopson's icon
Max Patch
Copy patch and select New From Clipboard in Max.

Instead of lopping off the highest value until you reach your target, I started by slicing off the lowest values and accumulating them until the target is met. Not sure why I took this approach, but I hope it meets your needs!
Holland

prof_lofi's icon

You are a star sir, whatever works! Will have a look at this first thing tomorrow (it's 1am here)...thanks Holland :)

Bill