From: Rick DeNatale Date: 2009-05-18T09:39:23+09:00 Subject: Re: permute each element of a ragged array? On Sun, May 17, 2009 at 7:10 PM, Phlip 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? I can't say I understand the pattern what about %w(android clown phenomenauts) %w(android posse phenomenauts) Would they be in the sequence? Does order matter, what about permutations[5] etc. -- Rick DeNatale Blog: http://talklikeaduck.denhaven2.com/ Twitter: http://twitter.com/RickDeNatale WWR: http://www.workingwithrails.com/person/9021-rick-denatale LinkedIn: http://www.linkedin.com/in/rickdenatale