Todd Benson
2/7/2008 12:22:00 AM
On Feb 6, 2008 5:20 PM, Adam Akhtar <adamtemporary@gmail.com> wrote:
> As some of you may know from previous threads im trying to practice
> specific areas of ruby. One simple exercise i set myself was taking a
> elements in an array such as
>
> [1,2,3,1,2,2,4,3,2,1]
>
> and then grouping them together and putting them in subarrays within an
> array like this
>
> [ [1,1,1], [2,2,2,2] , [3,3] ,[4] ]
>
> Heres how i did it (im new to both programming and ruby)
>
> def subpack(list)
> uniqlist = Array.new
> subpacklist = Array.new
> list.sort!
>
> uniqlist = list.uniq
> uniqlist.each_index do |i|
> rangeleft = list.index(uniqlist[i])
> rangeright = list.rindex(uniqlist[i])
> subpacklist << list.slice(rangeleft..rangeright)
> end
> subpacklist
> end
>
>
> WITHOUT GIVING ME YOUR SOLUTION IN FULL, how could i improve this...
> i.e. give me some hints, the names of some methods but not the solution
> as i want a go myself.
My solution uses #uniq, #each, #<<, #select, and #==. Oh, and #[]
Todd