Sunday, 15 September 2013

Algorithm/DataStructure to use to assign labels to entities based on their start and end times (Ruby)

Algorithm/DataStructure to use to assign labels to entities based on their
start and end times (Ruby)

I have a Ruby script that I inherited, where its reading a csv file
containing "TV programs" that have a start time, and end time in this
format:
start_time = 20:00:00
end_time = 20:45:00
The goal is to assign each TV program a "time-slot" (one of the following
values) based on the start and end times:
23:00:00 - 05:00:00 = Late Night = l
05:00:00 - 09:00:00 = Morning = m
09:00:00 - 17:00:00 = Day Time = d
17:00:00 - 20:00:00 = Evening = e
20:00:00 - 23:00:00 = Prime = p
Right now I have a giant if/else statement that is about 100 lines of Ruby
Code:
if(start_time >= 50000 && start_time < 90000) #start time is between 5
and 9 am
if(end_time <= 90000)
@timeSlot = ["Morning"]
puts "timeSlot = [Morning]"
elsif(end_time <= 170000 && end_time > 90000)
@timeSlot = ["Morning", "Daytime"]
puts "timeSlot = [Morning, Daytime]"
elsif(end_time <= 200000 && end_time > 90000 && end_time > 170000)
@timeSlot =["Morning", "Daytime", "Evening"]
puts "timeSlot =[Morning, Daytime, Evening]"
elsif(end_time <= 230000 && end_time > 90000 && end_time > 170000 &&
end_time > 200000)
@timeSlot =["Morning", "Daytime", "Evening", "Prime"]
puts "timeSlot =[Morning, Daytime, Evening, Prime]"
else
@timeSlot =["Morning", "Daytime", "Evening", "Prime", "LateNight"]
puts "timeSlot =[Morning, Daytime, Evening, Prime, LateNight]"
end
elsif (start_time >= 90000 && start_time < 170000)
.........
........
end
Im trying to change the implementation so the code is easy to maintain and
extend and read. My first try at this problem was to solve it visually
using a matrix in excel as shown.

This is the problem displayed visually. Now the question is how to do this
in code in an efficient way?
Any advice is welcome

No comments:

Post a Comment