[英]Making hashes find each other by their values using Ruby
我在此數組中有幾個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' }
#
]
但是我的算法似乎不好。 感謝您為這項工作提供的見識。
這將通過連續的結束和開始位置“加入”您的時間序列。
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"]]
您不是真的在尋找最長的線性后續時間表嗎? 我想通過評論來澄清這一點,但我無權發表評論。 在輸入和輸出中B的start_date(和start_location B)的值之間也存在差異,所以我假設這是一個錯誤。
考慮到您想要找到最長的線性后續時間表,我已經編寫了解決方案。
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"}
]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.