Random Number List Divide Segments with Sum Threshold

unhandled

(Dimitrigudgenov) #1

Hey There,

Want to subdivide a list of random numbers so that within the subdivided partitions the sum of the numbers is as close to but not above an adjustable threshold. In practice the threshold would always be greater than the largest random number. For example:

Take a random list of numbers:

4
2
11
2
1
6
3
6
2
5
1
4
8
7

Subdivide to several lists with a maximum sum threshold.

Threshold = 12

4,2 (sum = 6)
11 (sum = 11)
2,1,6,3 (sum = 12)
6, 2 (sum= 8)
5, 1, 4 (sum = 10)
8 (sum = 8)
7 (sum = 7)

Is there a way to automate this process. Maybe a script?

Tried using Hoopsnake and Anemone but I’m stuck.

Would appreciate any help.

Thanks,

D


(Laurent Delrieu) #2

As I needed something simple, a little C#. Just hope there not errors !!


DataTree output = new DataTree();
int sum = 0;
int path = 0;
foreach(int val in x)
{
if ((sum + val) > limit)
{
sum = 0;
path += 1;
}
sum = sum + val;
output.Add(val, new GH_Path(path));
}
A = output;
limit int addition.gh (14.7 KB)


Split / partition mass addition lists
(Dimitrigudgenov) #3

Thank you so much! Works great. Knew this would be easy with a bit more coding knowledge!
-D


(Dimitrigudgenov) #4

Is there a way to do the same thing but with decimals? I think integers would work fine for what I’m doing but just in case…

Thanks again for your help!

-D


(Laurent Delrieu) #5

Uhmm :thinking: just change the int to double and it must work!!!

limit double addition.gh (14.5 KB)


(Dimitrigudgenov) #6

Thanks again! You’re the best.
-D


(j porral) #7

I know I’m late, but thought I would give it a try with Anemone just for fun.
SplitListByNumberLimit.gh (17.1 KB)