I have a couple time_tables
in this array. There are four time_tables
that are related to each other in a linear way by their start_location - end_location
and start_date - end_date
.
When the first time_table
ends, the other time_table
starts, and so on.
My code:
arr = [
{ name: 01, start_date: '2014-04-24 22:03:00', start_location: 'A', end_date: '2014-04-24 22:10:00', end_location: 'B' },
{ name: 05, start_date: '2014-04-24 22:10:00', start_location: 'C', end_date: '2014-04-24 23:10:00', end_location: 'D' },
{ name: 01, start_date: '2014-04-24 17:10:00', start_location: 'X', end_date: '2014-04-24 20:10:00', end_location: 'B' },
{ name: 01, start_date: '2014-04-24 17:10:00', start_location: 'Z', end_date: '2014-04-24 20:10:00', end_location: 'B' },
{ name: 06, start_date: '2014-04-24 20:15:00', start_location: 'B', end_date: '2014-04-24 22:10:00', end_location: 'C' },
{ name: 03, start_date: '2014-04-24 23:15:00', start_location: 'D', end_date: '2014-04-24 00:10:00', end_location: 'E' }
]
new_array = []
i = 0
while i <= 5 do
if arr[i][:end_location] == arr[i+1][:start_location] && arr[i][:start_date] <= arr[i+1][:start_date]
new_array << arr[i+1]
end
i = i + 1
end
This is the result that I want:
# My expexpected result will be this:
# [
# { name: 01, start_date: '2014-04-24 22:03:00', start_location: 'A', end_date: '2014-04-24 22:10:00', end_location: 'B' },
# { name: 06, start_date: '2014-04-24 22:15:00', start_location: 'B', end_date: '2014-04-24 22:20:00', end_location: 'C' },
# { name: 05, start_date: '2014-04-24 22:20:00', start_location: 'C', end_date: '2014-04-24 23:10:00', end_location: 'D' },
# { name: 03, start_date: '2014-04-24 23:15:00', start_location: 'D', end_date: '2014-04-24 00:10:00', end_location: 'E' }
#
]
but my algorithm is seems to be bad. Thank you for insights to make this work.
This will "join" your time series by consecutive end and start location.
def span x; x[:end_location].ord - x[:start_location].ord; end
def diff x, y; x[:start_location].ord - y[:start_location].ord; end
arr = arr.sort_by { |x| [x[:start_location], span(x)] }
prev = arr[0]
arr = arr.slice_before { |e|
prev, prev2 = e, prev
diff(prev, prev2) != 0
}.to_a.map(&:first).chunk(&method(:span)).first[1]
For example, I get
arr.map { |x| [x[:start_location], x[:end_location] }
=> [["A", "B"], ["B", "C"], ["C", "D"], ["D", "E"]]
Aren't you really looking for the longest linear sub sequent time tables ? I wanted to clarify that through comments but I didn't have the permission to comment. Also there is a difference between the value of start_date of B (and start_location B) in input and the output, so I'm assuming it's a mistake.
I have written the solution considering you want to find the longest linear sub sequent time tables.
require 'date'
def getLLTT(time_tables)
longest = []
time_tables.sort_by! do |time_table|
DateTime.parse(time_table[:start_date]).to_time
end
0.upto(time_tables.size-1) do |i|
long_for_i = [time_tables[i]]
0.upto(i-1) do |j|
j_end_date = DateTime.parse(longest[j][-1][:end_date]).to_time
i_start_date = DateTime.parse(time_tables[i][:start_date]).to_time
if j_end_date <= i_start_date
if longest[j][-1][:end_location].eql? time_tables[i][:start_location]
if longest[j].size + 1 > long_for_i.size
long_for_i = longest[j] + [time_tables[i]]
end
end
end
end
longest[i] = long_for_i
end
return longest[-1]
end
puts getLLTT(arr)
So given the input :
arr = [
{ name: 01, start_date: '2014-04-24 22:03:00', start_location: 'A', end_date: '2014-04-24 22:10:00', end_location: 'B' },
{ name: 05, start_date: '2014-04-24 22:10:00', start_location: 'C', end_date: '2014-04-24 23:10:00', end_location: 'D' },
{ name: 01, start_date: '2014-04-24 17:10:00', start_location: 'X', end_date: '2014-04-24 20:10:00', end_location: 'B' },
{ name: 01, start_date: '2014-04-24 17:10:00', start_location: 'Z', end_date: '2014-04-24 20:10:00', end_location: 'B' },
{ name: 06, start_date: '2014-04-24 20:15:00', start_location: 'B', end_date: '2014-04-24 22:10:00', end_location: 'C' },
{ name: 03, start_date: '2014-04-24 23:15:00', start_location: 'D', end_date: '2014-04-24 00:10:00', end_location: 'E' }
]
The output will be :
[
{:name=>1, :start_date=>"2014-04-24 17:10:00", :start_location=>"Z", :end_date=>"2014-04-24 20:10:00", :end_location=>"B"}
{:name=>6, :start_date=>"2014-04-24 20:15:00", :start_location=>"B", :end_date=>"2014-04-24 22:10:00", :end_location=>"C"}
{:name=>5, :start_date=>"2014-04-24 22:10:00", :start_location=>"C", :end_date=>"2014-04-24 23:10:00", :end_location=>"D"}
{:name=>3, :start_date=>"2014-04-24 23:15:00", :start_location=>"D", :end_date=>"2014-04-24 00:10:00", :end_location=>"E"}
]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.