Marcin R. wrote:
On Monday 30 April 2007 02:55, Dan Z. wrote:
l = a.length
a.each_index { |x| a[x] = a[rand(l)] }a.each_index { |x| r= rand(l); a[x], a[r] = a[r], a[x] }
I forgot you could do that. No wonder arrays don’t have a swap method!
Marcin R. wrote:
On Monday 30 April 2007 02:55, Dan Z. wrote:
l = a.length
a.each_index { |x| a[x] = a[rand(l)] }a.each_index { |x| r= rand(l); a[x], a[r] = a[r], a[x] }
I forgot you could do that. No wonder arrays don’t have a swap method!
Peter S. wrote:
sort key. For sort_by, they’re cached; sort { rand <=> rand } might
be unpredictable, though.
That did it–I replaced calls to swap!() with three lines of code, and
now my algorithm is faster on arrays with as little as 40 elements!
Dan
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs