From: Mark Thomas Date: 2009-05-18T23:35:07+09:00 Subject: Re: permute each element of a ragged array? Not sure why the test must be as written, because the order doesn't fall in a logical pattern (as far as I can tell). Here's one that returns all the correct answers, but not in the prescribed order: def permute_sets(sets) return [sets, ""] if sets.size < 2 items = sets.shift << "" balance = permute_sets(sets) output = [] items.each do |item| balance.each do |bal| output << "#{item} #{bal}".strip end end return output end Output: android insane phenomenauts android insane android clown phenomenauts android clown android posse phenomenauts android posse android phenomenauts android hero insane phenomenauts hero insane hero clown phenomenauts hero clown hero posse phenomenauts hero posse hero phenomenauts hero insane phenomenauts insane clown phenomenauts clown posse phenomenauts posse phenomenauts (The last line is the empty string)