[Enumerable] positioning

Hello!

I’ve got this “homework”, I should create a ruby script that is able to
combine every possible letter in an array like

%w [ a b c d e f g h i j k ]

I should finally have all the possible results printed from

aaaaa
aaaab
aaaac

…to…

kkkkk

I guess I have to use an Enumerable and play with each letter’s
position… but I can’t imagine how :-\

Any idea?

Thanks,
Leo

Leo,

I hope I could simplify this problem to just find all the combinations
of characters. The final printing seems not very related to the puzzle.

Here is my thoughts,

You want e.g [‘a’, ‘b’, ‘c’, ‘d’] and all the combinations of different
characters, so that means you will get:

-1 diff
a | b | c | d

-2 diff
ab | ac | ad
| bc | bd
| cd
-3 diff
abc | acd
| bcd

-4 diff
abcd

You see the pattern? So it is quit easy to make an iterative algorithm
to find these combinations. Or I think you might use recursive idea to
find the answers if you put it this way:

  • Of all the 2 chars combinations that start with ‘a’, the second char
    must be from rest of the array. This way, e.g. [‘a’, ‘b’, ‘c’, ‘d’] will
    first find ‘cd’ then ‘bc’, ‘bd’ then ‘ab’, ‘ac’, ‘ad’

Hope that’s what you want…

Hello!
Uhm, what I’m trying to do is to have all the possible combination even
including the same character more and more time in the so generated
word.

So, something like

array = %w[ a b c d ]

aaaa
aaab
aaac
aaad
aaba
aabb

and so on.

Wow Hans, I guess you learn something new everyday. :stuck_out_tongue:

did you want?

array.repeated_permutation(4).map(&:join)