# Variable Sequence

Hi,
I have an issue with creating all possible outcomes for variable sequence of numbers.
For example, if i have 3 branches with 5, 7 and 9 values respectively, how do i create all possible combinations within each branch? The amount of numbers in each branch is different.

I’m not sure how to do it with components, so maybe there is a script that i can use?
Karlis

Can you… explain it again?

Your 2 pictures really don’t help much…
For example, with a tree like:
{0}
(0) 3
(1) 5
(2) 7

{1}
(0) 11
(1) 20
(2) 37

{2}
(0) 1.5
(1) 7
(2) 8
(3) 19
What would be the resulting tree?

1 Like

@john_9995 recently posted a solution which might meet your requirements:

Riccardo,
In the ideal scenario each branch would give me all possible combinations with each value.
i.e. 3,5,7 35,7 37,5 57,3

Karlis

Still don’t get it, please write down the full tree before and after. To take the first part of @maje90’s example:

\{0\} (3, 5, 7)

Is that supposed to look like:

\{0;0\} (3, 5, 7)
\{0;1\} (35, 7)
\{0;2\} (37, 5)
\{0;3\} (57, 3)

?

If so the values are treated as text and just concatenated? Why is there no 357? Why does the 3 come after the 57 in the last branch instead of before?

Before
{0} (3,5,7)

After
{0;0} (3,5,7)
{0;1} (35,7)
{0;2} (3,75)

The idea is that in a branch which has x amount of values i find all of the combinations of numbers that are next to each other. In this example i would not need (37,5). The values that are not close, for example in a branch (1,2,3,4), i would not need combinations (13,2,4) and (1,24,3), however (123,4) and (1,234) would work.
In short all combinations except the ones that aren’t next to each other.

At the moment it doesn’t matter if the combinations are placed in one or separate branches.

Karlis

1 Like

Yeah tricky, vanilla GH doesn’t have good support for creating permutations.
The attached file contains a (rather messy) C# script with a solution.

permutations.gh (9.7 KB)

``````  private void RunScript(List<string> E, ref object P)
{
var permutations = new List<string>();
int n = E.Count;
int pow = (int) Math.Pow(2, n - 1);

for (int pattern = 0; pattern < pow; pattern++)
{
string binary = Convert.ToString(pattern, 2);
binary = new string('0', n) + binary;
binary = binary.Substring(binary.Length - n + 1);

var permutation = string.Empty;
for (int i = 0; i < n; i++)
{
permutation += E[i];

if (i < n - 1)
if (binary[i] == '0')
permutation += ",";
}
}

P = permutations;
}
``````

the basic idea is to create all the binary numbers up to 2^{n-1} where n is the number of elements in your initial list. So let’s say 0000, 0001, 0010, 0011, 0100, 0101, … Whenever there’s a zero we want a comma, whenever there’s a one we just want to glue the two elements together. We need to ditch the last digit as commas after the last element are not wanted. Then zip through the two collections and compose those textual sequences… I’m sure there’s a much neater solution, but I can’t think of one right now.

2 Likes

Thank you for the information and the script!

Oh wow! Now even your first table pic make sense… somehow.
I was imaging a really different concept XD