构建搜索模块:使用线性逻辑数据

时间:2014-07-14 13:30:06

标签: ruby algorithm logic

我基本上想要列出给定列车编号的下一个时刻表(time_tables)。 通过输入列车编号。从那里开始寻找其余的线性路线,火车将长期获得不同的时间表)。当第一个time_table结束时,另一个time_table开始,依此类推。

现在我很快就会进入内循环:

  

未定义的方法`[]' for nil:NilClass(NoMethodError)

require 'date'

Time_tables =   [
    { name: '01251', start_date: '2014-04-24 22:03:00', start_location: 'A', end_date: '2014-04-24 22:10:00', end_location: 'BC' },
    { name: '05012', start_date: '2014-04-24 22:20:00', start_location: 'RI', end_date: '2014-04-24 23:10:00', end_location: 'XX' },
    { name: '03232', start_date: '2014-04-24 17:10:00', start_location: 'X', end_date: '2014-04-24 20:10:00', end_location: 'B' },
    { name: '02435', start_date: '2014-04-24 17:10:00', start_location: 'Z', end_date: '2014-04-24 20:10:00', end_location: 'B' },
    { name: '04545', start_date: '2014-04-24 22:15:00', start_location: 'BC', end_date: '2014-04-24 22:20:00', end_location: 'RI' },
    { name: '03545', start_date: '2014-04-24 23:15:00', start_location: 'XX', end_date: '2014-04-25 00:10:00', end_location: 'E' }
]


def finding_next_stops(time_tables, train_number)
  #variables:
  @referenced_point
  joined_time_tables = []

  time_tables.sort_by! do |time_table|
    DateTime.parse(time_table[:start_date]).to_time
  end

  #looking for the given time_table
  time_tables.each  do |i|
    if i[:name] == train_number
      @referenced_point = i
    end
  end

  #adding the time_table to the container and then starting the search from this item

  0.upto(time_tables.size-1) do |i|
    #If it's empty I wanna start with the referenced_point(this time_table)
    if joined_time_tables.empty?
      p  'gets in once!'
      middle_hand_container = @referenced_point
    else
      middle_hand_container = [time_tables[i]]
    end


    0.upto(i-1) do |j|
      j_end_date = joined_time_tables[j][-1][:end_date]
      i_start_date = time_tables[i][:start_date]
      if j_end_date <= i_start_date
        if joined_time_tables[j][-1][:end_location].eql? time_tables[i][:start_location]
          #if longest[j].size + 1 > long_for_i.size  # shall I check this ?

          middle_hand_container = joined_time_tables[j] + [time_tables[i]]
          #end
        end
      end
    end
    joined_time_tables[i] = middle_hand_container
  end
  return joined_time_tables[-1]
end


finding_next_stops(Time_tables, '05012')

如果我输入:&#39; 05012&#39; - train_number我应该得到:

  • {name:&#39; 05012&#39;,start_date:&#39; 2014-04-24 22:20:00&#39;,start_location: &#39; RI&#39;,end_date:&#39; 2014-04-24 23:10:00&#39;,end_location:&#39; XX&#39; }, { 名称: &#39; 03545&#39;,start_date:&#39; 2014-04-24 23:15:00&#39;,start_location:&#39; XX&#39;, end_date:&#39; 2014-04-24 00:10:00&#39;,end_location:&#39; E&#39; }

如果我输入:&#39; 04545&#39; train_number我应该得到:

  • {name:&#39; 04545&#39;,start_date:&#39; 2014-04-24 22:15:00&#39;,start_location: &#39; BC&#39;,end_date:&#39; 2014-04-24 22:20:00&#39;,end_location:&#39; RI&#39; }, { 名称: &#39; 05012&#39;,start_date:&#39; 2014-04-24 22:20:00&#39;,start_location:&#39; RI&#39;, end_date:&#39; 2014-04-24 23:10:00&#39;,end_location:&#39; XX&#39; }, { 名称: &#39; 03545&#39;,start_date:&#39; 2014-04-24 23:15:00&#39;,start_location:&#39; XX&#39;, end_date:&#39; 2014-04-25 00:10:00&#39;,end_location:&#39; E&#39; }

1 个答案:

答案 0 :(得分:0)

你的问题在这里:

j_end_date = joined_time_tables[j][-1][:end_date]

join_time_tables包含一系列哈希值。 [j]返回一个哈希值,因此[-1]返回nil。您应该删除[j][-1](我不确定哪个,因为代码太复杂了。)

<强>更新

我认为你的目标@referenced_point是一个数组,但实际上并非如此:

time_tables.each  do |i|
  if i[:name] == train_number
    @referenced_point = i
  end
end

您应该将其包装在一个数组中,以便您的代码能够正常工作:

middle_hand_container = @referenced_point

但我确实认为您的代码需要严格的重构。