How to obtain combinations and permutations of elements

Hello everyone!
A simply put questions: starting from this

A A B B

how can you obtain these?

AABB BBAA ABBA BAAB ABAB BABA

I’m trying to obtain k-combinations and k-permutations directly in Grasshopper, any input would be highly appreciated.
I already figured out how to delete duplicate entries, so if your method gives back things like
AA BB and A A BB
as two different things, it’s perfectly fine for me.

Thank you in advance!

P

Hi @Pietro_Pedone,

you might try below in a python component:

import itertools

x = ["A","A","B","B"]

for p in set(itertools.permutations(x, 4)):
    print " ".join(p)

It removes duplicates and prints this:

B A A B
A B B A
B B A A
A A B B
A B A B
B A B A

_
c.

Thank you for the answer! Thought that scripting would solve it; unfortunately, I am less than acquainted with that… So I was wondering how to set up the component. How can I set it to input a variable given in an outside panel? (see image)

Thank you again!

Hi @Pietro_Pedone, below seems to work:

_
c.

It’s not elegant, but this works.

permutations.gh (12.8 KB)

1 Like

Wow, thank you both, @DavidRutten and @clement, very helpful! I’m just uploading this in case that someone else has the same question: here’s how they work side-by-side, clement’s (top) and David’s (bottom)

1 Like

The solution posted by @DavidRutten appears to be doing combinations with replacement.

We also had a thread over on the Grasshopper forum covering several approaches to this (also using GHPython). I imaging the examples provided there might help to answer these queries

1 Like