简体   繁体   English

二维数组第二元素上的Ruby插值搜索

[英]Ruby Interpolation Search on 2nd Element of a Two Dimensional Array

What I have below is a simple interpolation search on a single dimensional array $employee_list . 我下面的内容是对一维数组$employee_list的简单插值搜索。 The list is an ordered list of employees' id that might have gaps due to retirements. 该列表是一个有序的员工ID列表,由于退休原因,该ID可能会有缺口。

def exist?(id)
  lower = 0
  upper = $employee_list.length - 1
  while $employee_list[upper] != $employee_list[lower] && id >= $employee_list[lower] && id <= $employee_list[upper]
      middle = lower + ((id - $employee_list[lower]) * (upper - lower) / ($employee_list[upper] - $employee_list[lower]))
      if id > $employee_list[middle]
        lower = middle + 1
      elsif id < $employee_list[middle]
        upper = middle - 1
      else
        return true
      end
    end
  return false
end

Now I want to add a new element to the list and the 2nd element of the array will contain the employee birth year (ie $employee_list[id][birthyear] ). 现在,我想向列表中添加一个新元素,数组的第二个元素将包含员工的出生年份(即$employee_list[id][birthyear] )。 I am able to sort the array based on the birthyear and I would like to perform a interpolation search based on the birthyear and return the list of employee ids that have that particular birthyear. 我能够根据出生年份对数组进行排序,并且我想基于出生年份执行内插搜索,并返回具有该特定出生年份的员工ID列表。

Given the following employee_list : 给定以下employee_list

employee_list = [[19, 1992], [41, 1985], [12, 1958], [63, 1985]]

If you want to get the ids of the employees born in 1985 you just need to select those arrays whose last element is 1985, and get the first element of the filtered arrays: 如果要获取1985年出生的雇员的ids ,则只需选择最后一个元素为1985的那些数组,并获取过滤后的数组的第一个元素:

employee_list.select{|employee| employee.last == 1985}.map(&:first)
# => [41, 63]

Here's a modified version. 这是修改后的版本。 It returns one id if an employee with given year has been found, and nil if no employee has been found. 如果找到了给定年份的雇员,则返回一个 id如果没有找到雇员,则返回nil

You'll need a modified version of interpolation search if you need to return multiple ids for a year. 如果您需要返回一年的多个ID,则需要修改后的插值搜索版本。 Finally, interpolation search works best with uniformly distributed values. 最后,插值搜索最适合均匀分布的值。 I guess it's really not the case for birthyear of employees. 我想员工的出生年确实不是这样。

def find_id_for_year(array, year)
  lower = 0
  upper = array.length - 1
  while array[upper][1] != array[lower][1] && year >= array[lower][1]  && year <= array[upper][1]
    middle = lower + ((year - array[lower][1]) * (upper - lower) / (array[upper][1] - array[lower][1]))
    if year > array[middle][1]
      lower = middle + 1
    elsif year < array[middle][1]
      upper = middle - 1
    else
      return array[middle][0]
    end
  end
end

employee_list = Array.new(10) {|i| [i, rand(1990..2000)] }.sort_by(&:last)

p employee_list
#=> [[5, 1990], [8, 1990], [2, 1991], [9, 1991], [7, 1992], [0, 1995], [6, 1996], [4, 1998], [1, 1999], [3, 1999]]
#=> [[8, 1990], [4, 1991], [5, 1991], [6, 1991], [2, 1996], [0, 1998], [1, 1998], [3, 1998], [7, 1999], [9, 2000]]

p find_id_for_year(employee_list, 1992)
#=> 7
#=> nil

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM