Array Practice

A

Adam Akhtar

a few people have mentioned some pretty concise solutions involving
map/collect selete find_all etc

can someone give me an example of such a solution using the above.

thanks
 
C

Christopher Dicely

a few people have mentioned some pretty concise solutions involving
map/collect selete find_all etc

can someone give me an example of such a solution using the above.

thanks

irb(main):067:0> def subpack(list)
irb(main):068:1> list.uniq.map { |item| [item] * list.find_all {
|val| val == item}.size }
irb(main):069:1> end
=> nil
irb(main):070:0> subpack [1,2,3,1,2,2,4,3,2,1]
=> [[1, 1, 1], [2, 2, 2, 2], [3, 3], [4]]
 
P

PullMonkey

[Note: parts of this message were removed to make it a legal post.]

Adam,

1) 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]]

2) 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}]

Charlie


a few people have mentioned some pretty concise solutions involving
map/collect selete find_all etc

can someone give me an example of such a solution using the above.

thanks

irb(main):067:0> def subpack(list)
irb(main):068:1> list.uniq.map { |item| [item] * list.find_all {
|val| val == item}.size }
irb(main):069:1> end
=> nil
irb(main):070:0> subpack [1,2,3,1,2,2,4,3,2,1]
=> [[1, 1, 1], [2, 2, 2, 2], [3, 3], [4]]
 
A

Adam Akhtar

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.

moving on....
 
A

Adam Akhtar

....to doing the opposite.

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.
 
S

Siep Korteling

Adam said:
....to doing the opposite.

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" .

Regards,

Siep
 
A

Adam Akhtar

Siep said:
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.

Thanks
adam.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,785
Messages
2,569,624
Members
45,318
Latest member
LuisWestma

Latest Threads

Top