How to obtain combinations and permutations of elements

unhandled
math

#1

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


#2

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.


#3

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!


#4

Hi @Pietro_Pedone, below seems to work:

_
c.


(David Rutten) #5

It’s not elegant, but this works.

permutations.gh (12.8 KB)


#6

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)


(Nathan 'jesterKing' Letwory) #8

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


#9

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