From: Mark Thomas Date: 2009-05-18T22:10:07+09:00 Subject: Re: permute each element of a ragged array? On May 17, 7:05 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? > > -- >    Phlip Why does your test not show %w{ android } ? Is the last set the only one that can be nil?