Skip to content

Instantly share code, notes, and snippets.

@imouaddine
Created November 19, 2014 21:21
Show Gist options
  • Save imouaddine/48c2d480ac62e64551de to your computer and use it in GitHub Desktop.
Save imouaddine/48c2d480ac62e64551de to your computer and use it in GitHub Desktop.
class Activity
attr_accessor :s, :f
def initialize(s,f)
@s = s
@f = f
end
def inspect
"#{s} to #{f}"
end
end
a = []
a << Activity.new(1,2)
a << Activity.new(3,4)
a << Activity.new(0,6)
a << Activity.new(5,7)
a << Activity.new(8,9)
a << Activity.new(5,9)
def select_activities(a)
#sort by finish time
a = a.sort_by(&:f)
#select first activity
result = []
result << a.first
#for each remaining activity
i = 1
until i == a.length
if result.last.f <= a[i].s
result << a[i]
end
i += 1
end
result
end
puts select_activities(a).to_s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment