Mark Thomas
5/18/2009 1:07:00 PM
On May 17, 7:05 pm, Phlip <phlip2...@gmail.com> wrote:
> Rubies:
>
> Here's a programming chestnut. I suppose I could brute-force my way through
> this, but I'm curious if anyone knows a slick or clever way to do it.
>
> We are talking about returning an array of arrays, each containing each
> permutation of the elements in the input array of arrays, including no element:
>
> def permute_sets(sets)
> # ?
> end
>
> test 'permute sets' do
> sets = [
> %w( android hero ),
> %w( insane clown posse ),
> %w( phenomenauts ),
> ]
> permutations = permute_sets(sets)
> assert permutations[ 0] == %w( android insane phenomenauts )
> assert permutations[ 1] == %w( android insane )
> assert permutations[ 2] == %w( android clown )
> assert permutations[ 3] == %w( android posse )
> assert permutations[ 4] == %w( hero insane phenomenauts )
> assert permutations[-1] == []
> end
>
> So, pass the test (generically, so any test with the same pattern would pass),
> and you win! Any ideas?
>
> --
> Phlip
Why does your test not show %w{ android } ? Is the last set the only
one that can be nil?