[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

array ordering / selection according to elements -

Shai Rosenfeld

11/13/2007 3:16:00 PM


given that time1 < time2 < time3 < time4 ... (and are all Time objects)

myarray= [ time1, time2, time3, time4, time5 , time6 , time7 , .. ]

special_sort(time4)= [ time3, time4, time5 ]

...basically, passing an arg of a certain obj in the array to a
"special_sort" func, how do i collect the "nearest" ones to it? thx for
any suggestions
--
Posted via http://www.ruby-....

4 Answers

Robert Klemme

11/13/2007 3:39:00 PM

0

2007/11/13, Shai Rosenfeld <shaiguitar@gmail.com>:
>
> given that time1 < time2 < time3 < time4 ... (and are all Time objects)
>
> myarray= [ time1, time2, time3, time4, time5 , time6 , time7 , .. ]
>
> special_sort(time4)= [ time3, time4, time5 ]
>
> ...basically, passing an arg of a certain obj in the array to a
> "special_sort" func, how do i collect the "nearest" ones to it? thx for
> any suggestions

Define "nearest". What happens if time4 is not present in the array
itself? etc.

Cheers

robert

--
use.inject do |as, often| as.you_can - without end

Dale Martenson

11/13/2007 3:40:00 PM

0

On Nov 13, 9:16 am, Shai Rosenfeld <shaigui...@gmail.com> wrote:
> given that time1 < time2 < time3 < time4 ... (and are all Time objects)
>
> myarray= [ time1, time2, time3, time4, time5 , time6 , time7 , .. ]
>
> special_sort(time4)= [ time3, time4, time5 ]
>
> ..basically, passing an arg of a certain obj in the array to a
> "special_sort" func, how do i collect the "nearest" ones to it? thx for
> any suggestions
> --
> Posted viahttp://www.ruby-....

You could do something like this. It only would work for exact
matches.

--Dale Martenson



class Array
def predecessor(index)
return nil if index == 0
self[index-1]
end
def successor(index)
return nil if index > (self.length-1)
self[index+1]
end
def near(value)
return nil unless index = self.index(value)
[ self.predecessor(index), self[index], successor(index) ]
end
end

a = [1, 2, 3, 4, 6, 7, 8, 9]

puts a.near(0).inspect # not found
puts a.near(1).inspect
puts a.near(2).inspect
puts a.near(3).inspect
puts a.near(4).inspect
puts a.near(5).inspect # not found
puts a.near(6).inspect
puts a.near(7).inspect
puts a.near(8).inspect
puts a.near(9).inspect
puts a.near(10).inspect # not found

Michel Boaventura

11/13/2007 3:48:00 PM

0

Try to include time4 on the array, then sort it. Lood in wich position
is time4, then get the time before e after it. Isn't very efficient,
but works :D

2007/11/13, Robert Klemme <shortcutter@googlemail.com>:
> 2007/11/13, Shai Rosenfeld <shaiguitar@gmail.com>:
> >
> > given that time1 < time2 < time3 < time4 ... (and are all Time objects)
> >
> > myarray= [ time1, time2, time3, time4, time5 , time6 , time7 , .. ]
> >
> > special_sort(time4)= [ time3, time4, time5 ]
> >
> > ...basically, passing an arg of a certain obj in the array to a
> > "special_sort" func, how do i collect the "nearest" ones to it? thx for
> > any suggestions
>
> Define "nearest". What happens if time4 is not present in the array
> itself? etc.
>
> Cheers
>
> robert
>
> --
> use.inject do |as, often| as.you_can - without end
>
>

Stefan Rusterholz

11/13/2007 3:56:00 PM

0

Shai Rosenfeld wrote:
>
> given that time1 < time2 < time3 < time4 ... (and are all Time objects)
>
> myarray= [ time1, time2, time3, time4, time5 , time6 , time7 , .. ]
>
> special_sort(time4)= [ time3, time4, time5 ]
>
> ...basically, passing an arg of a certain obj in the array to a
> "special_sort" func, how do i collect the "nearest" ones to it? thx for
> any suggestions

min is O(n), gives you only the closest:
array.min { |a,b| (closest_to - a).abs <=> (closest_to - b).abs }

sort_by is O(nlogn), but you can use first(n) to get the n closest
values:
array.sort_by { |e| (closest_to - e).abs }.first(n)

Regards
Stefan
--
Posted via http://www.ruby-....