我在这个数组中有一对time_tables
。有四个time_tables
start_location - end_location
和start_date - end_date
以线性方式相互关联。
当第一个time_table
结束时,另一个time_table
开始,依此类推。
我的代码:
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
这是我想要的结果:
# 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' }
#
]
但我的算法似乎很糟糕。感谢您提供有关此工作的见解。
答案 0 :(得分:0)
这将&#34;加入&#34;你的时间序列是连续的结束和开始的位置。
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]
例如,我得到了
arr.map { |x| [x[:start_location], x[:end_location] }
=> [["A", "B"], ["B", "C"], ["C", "D"], ["D", "E"]]
答案 1 :(得分:0)
Aren你真的在寻找最长的线性后续时间表吗?我想通过评论澄清一下,但我没有得到评论的许可。输入中的B(和start_location B)的start_date值与输出之间也存在差异,因此我假设它是错误的。
我已经编写了解决方案,考虑到你想要找到最长的线性后续时间表。
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)
所以给出了输入:
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' }
]
输出将是:
[
{: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"}
]