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
I guess I have to use an Enumerable and play with each letter’s
position… but I can’t imagine how :-\
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:
a | b | c | d
ab | ac | ad
| bc | bd
abc | acd
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…
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
So, something like
array = %w[ a b c d ]
and so on.
Wow Hans, I guess you learn something new everyday.
did you want?