Skip to content

Instantly share code, notes, and snippets.

@hlxwell
Last active November 28, 2015 12:56
Show Gist options
  • Save hlxwell/5d71f3c466a2cc9b0557 to your computer and use it in GitHub Desktop.
Save hlxwell/5d71f3c466a2cc9b0557 to your computer and use it in GitHub Desktop.
According to duration, split spots into different days
# ActiveSupport has it.
class Array
def sum
if self.size == 0
return 0
else
self.inject(:+)
end
end
end
## Demo for Spliting Days #################################
TOTAL_HOURS_A_DAY = 8
DURATIONS = [1,2,3,1,1,2,3,4,1,2,3,1,2,3,1,5,1,2,1,0.5,1,2]
splited_days = [[]]
DURATIONS.each do |duration, index|
# has to be within TOTAL_HOURS_A_DAY
# expected_trip_time = splited_days.last.sum + duration
# A little more is fine
expected_trip_time = splited_days.last.sum
if expected_trip_time >= TOTAL_HOURS_A_DAY
splited_days.push [duration]
else
splited_days.last.push duration
end
end
# Show the result
splited_days.each do |a|
puts a.inspect
puts a.sum
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment