Hi --

On Tue, 13 Dec 2005, sanha lee wrote:

> I think that it is better solution
>
> def all_sum(arr)
>  return arr if arr.length == 1
>  temp = arr[-1]
>  return all_sum(arr[0...-1]) + all_sum(arr[1...-1]).collect {|i| i + temp}
> end

I get a too-deep recursion with that too.  Here's another candidate:

   def all_sum(arr)
     case arr.size
     when 1 then []
     else arr[0..-2].map {|i| i + arr[-1]} + all_sum(arr[0..-2])
     end
   end

   p all_sum([1,2,3,4])
   => [5, 6, 7, 4, 5, 3]


David

-- 
David A. Black
dblack / wobblini.net

"Ruby for Rails", forthcoming from Manning Publications, April 2006!