watzon
watzon

Reputation: 2549

Get all possible permutations of bigger array

I am trying to take an array of strings ["-", "+", "*"] and figure out all combinations of them for a given size. This is possible by doing the following:

arr = ["-", "+", "*"]
perms = arr.permutation(2).to_a
# => [["-", "+"], ["-", "*"], ["+", "-"], ["+", "*"], ["*", "-"], ["*", "+"]]

However, I want to get results for an array bigger than the given array. E.g., arr.permutations(4).to_arr. Is this possible?

Upvotes: 0

Views: 86

Answers (2)

sawa
sawa

Reputation: 168081

Yes, it's possible.

perms = arr.repeated_permutation(4).to_a

Upvotes: 4

David Hoelzer
David Hoelzer

Reputation: 16331

No, not through any built in library or statistical method.

The Combination and Permutation methods implement standard probability theory functions. That is, "A things taken B at a time." You cannot create combinations or permutations using these standard methods to take more "things" than you actually have in the set.

Upvotes: 0

Related Questions