Eli Hooten - 1 year ago 91

Ruby Question

Questions similar to this have been asked before on SO, but they're not quite what I need and I can't seem to arrive at my solution through altering/modifying those approaches.

In any case, I have an array of arrays, as follows:

`b= [["1"],["2"],["3"],["4"],["5"],["6"]]`

(If it makes it easier to arrive at a solution,

`b`

`["1","2","3","4","5","6"]`

and I would like to generate the following:

`[["123456"],["213456"],["312456"],...]`

where each array in the output array is a unique permutation of the six numbers. I would also take it as a single array (e.g.,

`["123456", "213456",...]`

`"123"`

As much as this sounds like it, this isn't a homework problem. I could brute for this thing and get the output I need. I just feel like there has to be a better, more elegant, solution.

Answer Source

With Array#permutation:

```
permutations = (1..6).to_a.permutation.map(&:join)
# ["123456", "123465", "123546", ..., "654312", "654321"]
```