-
-
Save jessiel-hacke/7cd8e46f62675b06bb1e1f6b5d7e8a3c to your computer and use it in GitHub Desktop.
test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'date' | |
require 'set' | |
def normalize_requests(requests) | |
start_work = DateTime.strptime('8', '%H').to_time | |
end_work = DateTime.strptime('17', '%H').to_time | |
requests = requests.map do |c| | |
c = c.split(',') | |
tp = [DateTime.strptime(c.first, '%H:%M%P').to_time, c.last.strip.to_i] | |
tp if tp.first >= start_work && !overlap(tp.first, tp.last, end_work) | |
end | |
requests.delete(nil) | |
requests = requests.uniq # not duplicates | |
requests.sort {|a,b| a[0] <=> b[0]} #sort based on the time | |
end | |
def overlap(start_date, duration, next_start_date) | |
start_date + ((duration+5) * 60) > next_start_date | |
end | |
def day_schedule(requests) | |
requests = normalize_requests(requests) | |
i = 0 | |
j = i + 1 | |
current_req = requests.first | |
final_schedule = Set.new | |
final_schedule << current_req | |
while i < requests.size && j < requests.size do | |
if overlap(current_req[0], current_req[1], requests[j][0]) | |
if current_req[1] > requests[j][1] | |
final_schedule << current_req | |
else | |
final_schedule.delete(current_req) | |
final_schedule << requests[j] | |
current_req = requests[j] | |
end | |
else | |
current_req = requests[j] | |
final_schedule << current_req | |
end | |
j += 1 | |
i += 1 | |
end | |
return final_schedule.map{|s| s[1]}.reduce(:+) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment