【问题标题】:building a search-module: using linear logic- data构建搜索模块:使用线性逻辑数据
【发布时间】:2014-07-14 13:30:05
【问题描述】:

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

现在我一进入内部循环就收到此错误:

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')

如果我输入:'05012'- train_number 我应该得到:

  • { 名称:'05012',开始日期:'2014-04-24 22:20:00',开始位置: 'RI', end_date: '2014-04-24 23:10:00', end_location: 'XX' }, { name: '03545',开始日期:'2014-04-24 23:15:00',开始位置:'XX', end_date: '2014-04-24 00:10:00', end_location: 'E' }

如果我输入:'04545' train_number 我应该得到:

  • { 名称:'04545',开始日期:'2014-04-24 22:15:00',开始位置: 'BC', end_date: '2014-04-24 22:20:00', end_location: 'RI' }, { name: '05012', start_date: '2014-04-24 22:20:00', start_location: 'RI', 结束日期:'2014-04-24 23:10:00',结束位置:'XX'},{名称: '03545',开始日期:'2014-04-24 23:15:00',开始位置:'XX', end_date: '2014-04-25 00:10:00', end_location: 'E' }

【问题讨论】:

    标签: ruby algorithm logic


    【解决方案1】:

    你的问题在这里:

    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
    

    不过,我确实认为您的代码需要认真重构。

    【讨论】:

    猜你喜欢
    • 2013-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-15
    • 2015-11-29
    • 1970-01-01
    相关资源
    最近更新 更多