Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Obtain the smalles n items from an Enumerable

View Enum_n_min_1.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
module Enumerable
def n_min(n)
raise "Illegal argument: #{n.inspect}" unless n > 0
mins = []
each do |item|
mins.push item
mins.sort!
mins.pop if mins.size > n
end
mins
end
end
View Enum_n_min_1.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
module Enumerable
def n_min(n)
raise "Illegal argument: #{n.inspect}" unless n > 0
 
mins = []
 
each do |item|
mins.size.times do |i|
if item < mins[i]
mins.insert i, item
break false
end
end and mins.push item
 
mins.pop if mins.size > n
end
 
mins
end
end
View Enum_n_min_1.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
module Enumerable
def n_min(n)
raise "Illegal argument: #{n.inspect}" unless n > 0
 
mins = []
 
each do |item|
mins.size.times do |i|
if item < mins[i]
mins.insert i, item
mins.pop if mins.size > n
break false
end
end and mins.size < n and mins.push item
end
 
mins
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.