Вот еще:
class String
def splitup(prefix=nil)
parts = []
if size <= 3
parts << [prefix,self].compact * ","
end
(1..([size,3].min)).each do |p|
next if p >= size
parts << slice(p..-1).splitup([prefix,slice(0,p)].compact * ",")
end
parts
end
def report
flat = splitup.flatten.sort_by {|x| [-x.size,x]}
puts
puts "#{flat.size} permutations of #{self}"
puts flat
end
end
, а затем
>> "123456".report
24 permutations of 123456
1,2,3,4,5,6
1,2,3,4,56
1,2,3,45,6
1,2,34,5,6
1,23,4,5,6
12,3,4,5,6
1,2,3,456
1,2,34,56
1,2,345,6
1,23,4,56
1,23,45,6
1,234,5,6
12,3,4,56
12,3,45,6
12,34,5,6
123,4,5,6
1,23,456
1,234,56
12,3,456
12,34,56
12,345,6
123,4,56
123,45,6
123,456