如何使用Ruby将这些时间表连接在一起?

时间:2014-07-12 22:49:28

标签: ruby algorithm logic

我的最终结果应该是这样的:

[
  { name: 01, start_date: '2014-04-24 22:03:00', start_location: 'A', end_date: '2014-04-24 22:10:00', end_location: 'BC' },
  { name: 06, start_date: '2014-04-24 22:15:00', start_location: 'BC', end_date: '2014-04-24 22:20:00', end_location: 'RI' },
  { name: 05, start_date: '2014-04-24 22:20:00', start_location: 'RI', end_date: '2014-04-24 23:10:00', end_location: 'XX' },
  { name: 03, start_date: '2014-04-24 23:15:00', start_location: 'XX', end_date: '2014-04-24 00:10:00', end_location: 'E' }
]

这是我的代码到目前为止,但不幸的是它不能正常工作:

arr =   [
  { name: 01, start_date: '2014-04-24 22:03:00', start_location: 'A', end_date: '2014-04-24 22:10:00', end_location: 'BC' },
  { name: 05, start_date: '2014-04-24 22:20:00', start_location: 'RI', end_date: '2014-04-24 23:10:00', end_location: 'XX' },
  { 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 22:15:00', start_location: 'BC', end_date: '2014-04-24 22:20:00', end_location: 'RI' },
  { name: 03, start_date: '2014-04-24 23:15:00', start_location: 'XX', 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]
#   end
#   i = i + 1
#   puts new_array
# end

很高兴获得一些如何解决这个问题的见解!

0 个答案:

没有答案