[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Iterate through array twice

Ruhe

2/16/2008 2:47:00 PM

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.
3 Answers

Robert Klemme

2/16/2008 3:18:00 PM

0

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.

Christopher Dicely

2/16/2008 6:20:00 PM

0

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 <nocturneer@gmail.com> 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.
>
>

Ruhe

2/16/2008 7:16:00 PM

0

Yes, Ruby is the most beautiful language!

On 16 ???, 21:20, Christopher Dicely <cmdic...@gmail.com> 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 <nocturn...@gmail.com> wrote: