Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Iterate through array twice

Reply
Thread Tools

Iterate through array twice

 
 
Ruhe
Guest
Posts: n/a
 
      02-16-2008
I have array of horizontal segments and I need to find which of them
may be sides of a box, so I implemented this method:

def find_square_sides(horiz_segments)
squares = Array.new
horiz_segments.each_with_index do |side, i|
horiz_segments[(i+1)..(horiz_segments.size - 1)].each do |
condidate|
if(## here goes long-long check ##)
squares << Square.new(side, condidate)
end
end
end
squares
end


Knowing the beauty of Ruby, I hope that there is a better solution. I
guess that double iterating isn't the best.
 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      02-16-2008
On 16.02.2008 15:46, Ruhe wrote:
> I have array of horizontal segments and I need to find which of them
> may be sides of a box, so I implemented this method:
>
> def find_square_sides(horiz_segments)
> squares = Array.new
> horiz_segments.each_with_index do |side, i|
> horiz_segments[(i+1)..(horiz_segments.size - 1)].each do |
> condidate|
> if(## here goes long-long check ##)
> squares << Square.new(side, condidate)
> end
> end
> end
> squares
> end
>
>
> Knowing the beauty of Ruby, I hope that there is a better solution. I
> guess that double iterating isn't the best.


Unfortunately the problem you are trying to solve is O(n*n) and there is
nothing that will make this go away - at least not with the knowledge we
have so far. It may be that elements to compare to can be picked more
efficiently but if you have to compare each with each other your
solution is probably as good as it gets.

Kind regards

robert


PS: The only small improvement that came to mind was to use -1 as ending
index for the range of the second iteration.
 
Reply With Quote
 
 
 
 
Christopher Dicely
Guest
Posts: n/a
 
      02-16-2008
With Ruby 1.9:

def find_square_sides(horiz_segments)
squares = Array.new
horiz_segments.combination(2) do |side, candidate|
if (...whatever...)
squares << Square.new(side, candidate)
end
end
end

On Feb 16, 2008 6:49 AM, Ruhe <(E-Mail Removed)> wrote:
> I have array of horizontal segments and I need to find which of them
> may be sides of a box, so I implemented this method:
>
> def find_square_sides(horiz_segments)
> squares = Array.new
> horiz_segments.each_with_index do |side, i|
> horiz_segments[(i+1)..(horiz_segments.size - 1)].each do |
> condidate|
> if(## here goes long-long check ##)
> squares << Square.new(side, condidate)
> end
> end
> end
> squares
> end
>
>
> Knowing the beauty of Ruby, I hope that there is a better solution. I
> guess that double iterating isn't the best.
>
>


 
Reply With Quote
 
Ruhe
Guest
Posts: n/a
 
      02-16-2008
Yes, Ruby is the most beautiful language!

On 16 ΖΕΧ, 21:20, Christopher Dicely <(E-Mail Removed)> wrote:
> With Ruby 1.9:
>
> def find_square_sides(horiz_segments)
> squares = Array.new
> horiz_segments.combination(2) do |side, candidate|
> if (...whatever...)
> squares << Square.new(side, candidate)
> end
> end
> end
>
> On Feb 16, 2008 6:49 AM, Ruhe <(E-Mail Removed)> wrote:


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to iterate 2 nested collections w <logic:iterate> without a"getter" John Java 4 04-01-2008 09:46 AM
iterate through an array of method names to an object Skye Weir-mathewes Ruby 3 04-17-2007 12:38 PM
nested:iterate or logic: iterate with multibox?? runescience Java 0 02-09-2006 12:57 AM
ASP Classes: How do I iterate through a property that's an array? ASP General 1 04-18-2005 10:33 PM
<logic:iterate /> iterate beyond items in the collection Gogo Java 1 09-04-2003 08:40 PM



Advertisments