制作散列发现对方使用的Ruby它们的值 [英] Making hashes find each other by their values using Ruby

查看:109
本文介绍了制作散列发现对方使用的Ruby它们的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这阵一对夫妇 time_tables 。有四个 time_tables 是由它们的 START_LOCATION相互关联的以线性的方式 - END_LOCATION START_DATE - END_DATE

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.

在第一个 time_table 结束,其他 time_table 开始,依此类推。

When the first time_table ends, the other time_table starts, and so on.

我的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

这就是我想要的结果:

  # 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.

推荐答案

你是不是真的找最长的直子序贯的时间表?我想澄清的是,通过评论,但我没有发表评论的权限。也有B(和START_LOCATION B)在输入和输出的START_DATE的值之间的差,所以我假定这是一个错误。

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' }
]

输出将是:

[
{: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"}
]

这篇关于制作散列发现对方使用的Ruby它们的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆