[ [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.
Here it is is with uniq,map and select
pullmonkey:~$ irb
irb(main):001:0> a = [1,2,1,3,2,1,3,2]
=> [1, 2, 1, 3, 2, 1, 3, 2]
irb(main):002:0> a.uniq.map{|x| a.select{|y| y == x}}
=> [[1, 1, 1], [2, 2, 2], [3, 3]]
And for a hash of element frequency using uniq,map and select
irb(main):004:0> a.uniq.map{|x| {x => a.select{|y| y == x}.size}}
=> [{1=>3}, {2=>3}, {3=>2}]
Thanks guys, i actually thought that you were doing something allong
those lines but i wasnt sure if the nested block could access the parent
block i.e.
in
a.uniq.map{|x| {x => a.select{|y| y == x}.size}}
specifically the part
{|y| y == x}.size}
x is from the parent block. I thought the nested block couldnt see it. I
was too lazy to try! Ill start to try and use collect/map
find_all/select in my code a bit more from now on.
Although there is already a method call to flatten arrays i thought
since i created an unflatten one above i should have go at flattening.
It takes a nested array and then returns a flattened version
def flattenlist nestedlist
flatlist = []
return flatlist if nestedlist.empty?
if (nestedlist.first.is_a?(Array))
flatlist.push nestedlist.first.shift
nestedlist.delete_at(0) if (nestedlist.first.empty?)
else #not array so just add
flatlist.push nestedlist.shift
end
flatlist = flatlist + flattenlist(nestedlist)
flatlist
end
What do you guys/girls think? give me your hints and ill go away and
improve it!
I wasnt so happy with this part
if (nestedlist.first.is_a?(Array))
flatlist.push nestedlist.first.shift
nestedlist.delete_at(0) if (nestedlist.first.empty?)
I have a feeling theres a delete method that after deleting the last
element in an empty array will delete the array itself.
You want to flatten this array? Hey, it’s just there. If you want to
know how it works, find a cmd-prompt and type “ri Array.flatten” .
Regards,
Siep
hi siep thanks for the reply. Yeah i know theres already a method but i
thought it would be good practice to try and create my own function. Ill
take a look at how ruby flattens arrays with that command prompt call
you mentioned.
Although there is already a method call to flatten arrays i thought
since i created an unflatten one above i should have go at flattening.
It takes a nested array and then returns a flattened version
def flattenlist nestedlist
flatlist = []
return flatlist if nestedlist.empty?
if (nestedlist.first.is_a?(Array))
flatlist.push nestedlist.first.shift
nestedlist.delete_at(0) if (nestedlist.first.empty?)
else #not array so just add
flatlist.push nestedlist.shift
end
flatlist = flatlist + flattenlist(nestedlist)
flatlist
end
What do you guys/girls think? give me your hints and ill go away and
improve it!
I wasnt so happy with this part
if (nestedlist.first.is_a?(Array))
flatlist.push nestedlist.first.shift
nestedlist.delete_at(0) if (nestedlist.first.empty?)
I have a feeling theres a delete method that after deleting the last
element in an empty array will delete the array itself.
If you’d like to know what methods are available for an object, try
this:
a=[[1,2],[1,3],2,3]
puts a.methods.sort
You want to flatten this array? Hey, it’s just there. If you want to
know how it works, find a cmd-prompt and type “ri Array.flatten” .