Rob Biedenharn wrote:
> On Feb 6, 2008, at 6:20 PM, Adam Akhtar wrote:
> 
>> [ [1,1,1], [2,2,2,2] , [3,3] ,[4] ]
>>    rangeleft = list.index(uniqlist[i])
>> as i want a go myself.
> Think about how you might do it with a pencil...
> - write the values out in order "[ 1, 1, 1, 2, 2, 2, 2, 3, 3, 4 ]"
> - if the "next" value is different from the last value processed (and
> initially there is no last value), then start a new sub-array with
> this single value (my pencil would insert a '[' between the '[' and '1')
> - ...and if you'd already written a '[', you have to then  write "],["
> - if the next value is the same, move your pencil (since it goes in
> the current sub-array)
> - when you run out of values, close the final subarray with ']'

well heres my way of doing the above:-

def subpackarray passedlist

  orderedlist = passedlist.sort
  subgrouplist = Array.new
  orderedlist.each_index do |i|
    if ((i == 0) || (orderedlist[i] != orderedlist[i-1]))
      #create a new subgroup array and add the element.
      subgrouplist << Array.new
      subgrouplist.last.push orderedlist[i]
    else
      #add element to existing subgroup
      subgrouplist.last.push orderedlist[i]
    end
  end
  subgrouplist
end


i think thats the way he was intending it. Ill start figuring out the 
others as well. Thanks so much for the posts. I think this makes great 
practice asking for hints rather than just reading someone elses code.


-- 
Posted via http://www.ruby-forum.com/.