Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 将两个有序数组合并为一个有序数组_Arrays_Ruby - Fatal编程技术网

Arrays 将两个有序数组合并为一个有序数组

Arrays 将两个有序数组合并为一个有序数组,arrays,ruby,Arrays,Ruby,我正在编写一个接受两个排序数组的方法,我希望它返回一个包含所有排序值的合并数组。鉴于以下两个阵列: array_one = [3, 4, 8] array_two = [1, 5, 7] 我希望我的merge_array方法返回: [1, 3, 4, 5, 7, 8] 我目前的算法如下: def merge_arrays(array_one, array_two) merged_array_size = array_one.length + array_two.length merg

我正在编写一个接受两个排序数组的方法,我希望它返回一个包含所有排序值的合并数组。鉴于以下两个阵列:

array_one = [3, 4, 8]
array_two = [1, 5, 7]
我希望我的merge_array方法返回:

[1, 3, 4, 5, 7, 8]
我目前的算法如下:

def merge_arrays(array_one, array_two)
  merged_array_size = array_one.length + array_two.length
  merged_array = []

  current_index_on_one = 0
  current_index_on_two = 0
  current_merged_index = 0

  for i in (0..merged_array_size - 1)
    if array_one[current_index_on_one] < array_two[current_index_on_two]
      merged_array[current_merged_index] = array_one[current_index_on_one]
      current_index_on_one += 1
      current_merged_index += 1
    else
      merged_array[current_merged_index] = array_two[current_index_on_two]
      current_index_on_two += 1
      current_merged_index += 1
    end
  end

  return merged_array
end
def merge_数组(数组一、数组二)
合并的数组大小=数组长度1.length+数组长度2.length
合并的_数组=[]
当前_索引_on_one=0
_二上的当前_索引_=0
当前合并索引=0
对于(0..merged_array_size-1)中的i
如果数组\u-one[当前\u-index\u-on\u-one]<数组\u-two[当前\u-index\u-on\u-two]
合并的\u数组[当前\u合并的\u索引]=数组\u一[当前的\u索引\u在\u一上]
当前_索引_on_one+=1
当前合并索引+=1
其他的
合并数组[当前索引]=数组二[当前索引二]
_二上的当前_索引_+=1
当前合并索引+=1
结束
结束
返回合并的数组
结束

我得到一个错误“undefined method”这里有一种方法可以使用递归编写
merge
。注意,正如您指定的,两个输入必须已经排序,否则输出将无效。输入的大小可能不同

def merge (xs, ys)
  if xs.empty?
    ys
  elsif ys.empty?
    xs
  else
    x, *_xs = xs
    y, *_ys = ys
    if x < y
      [x] + (merge _xs, ys)
    else
      [y] + (merge xs, _ys)
    end
  end
end

merge [ 1, 3, 4, 6, 8, 9 ], [ 0, 2, 5, 7 ]
# => [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
def合并(xs,ys)
如果xs.empty?
ys
艾尔希夫是空的吗?
xs
其他的
x、 *xs=xs
y、 *_ys=ys
如果x [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]

这里有一种方法可以使用递归编写
合并。注意,正如您指定的,两个输入必须已经排序,否则输出将无效。输入的大小可能不同

def merge (xs, ys)
  if xs.empty?
    ys
  elsif ys.empty?
    xs
  else
    x, *_xs = xs
    y, *_ys = ys
    if x < y
      [x] + (merge _xs, ys)
    else
      [y] + (merge xs, _ys)
    end
  end
end

merge [ 1, 3, 4, 6, 8, 9 ], [ 0, 2, 5, 7 ]
# => [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
def合并(xs,ys)
如果xs.empty?
ys
艾尔希夫是空的吗?
xs
其他的
x、 *xs=xs
y、 *_ys=ys
如果x [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]

也许我没有抓住要点,但你可以做到:

(array_one + array_two).sort
=> [1, 3, 4, 5, 7, 8]

也许我没有抓住要点,但你可以做到:

(array_one + array_two).sort
=> [1, 3, 4, 5, 7, 8]

假设有两个排序数组。您需要使用递归创建管道,以处理每个数组。在每次迭代中检查以查看 任一数组的索引
0
处的哪个值较低,从数组中删除该值并将该值附加到
结果
数组中

def merge_arrays(a, b)
  # build a holder array that is the size of both input arrays O(n) space
  result = []

  # get lower head value
  if a[0] < b[0]
    result << a.shift
  else
    result << b.shift
  end

  # check to see if either array is empty
  if a.length == 0
    return result + b
  elsif b.length == 0
    return result + a
  else
    return result + merge_arrays(a, b)
  end
end 

> a = [3, 4, 6, 10, 11, 15]
> b = [1, 5, 8, 12, 14, 19]
> merge_arrays(a, b)
#=> [1, 3, 4, 5, 6, 8, 10, 11, 12, 14, 15, 19]
def合并_阵列(a、b)
#构建一个大小与两个输入数组O(n)空间相同的holder数组
结果=[]
#获得较低的头值
如果a[0]合并_数组(a,b)
#=> [1, 3, 4, 5, 6, 8, 10, 11, 12, 14, 15, 19]

假设您有两个排序数组。您需要使用递归创建管道,以处理每个数组。在每次迭代中检查以查看 任一数组的索引
0
处的哪个值较低,从数组中删除该值并将该值附加到
结果
数组中

def merge_arrays(a, b)
  # build a holder array that is the size of both input arrays O(n) space
  result = []

  # get lower head value
  if a[0] < b[0]
    result << a.shift
  else
    result << b.shift
  end

  # check to see if either array is empty
  if a.length == 0
    return result + b
  elsif b.length == 0
    return result + a
  else
    return result + merge_arrays(a, b)
  end
end 

> a = [3, 4, 6, 10, 11, 15]
> b = [1, 5, 8, 12, 14, 19]
> merge_arrays(a, b)
#=> [1, 3, 4, 5, 6, 8, 10, 11, 12, 14, 15, 19]
def合并_阵列(a、b)
#构建一个大小与两个输入数组O(n)空间相同的holder数组
结果=[]
#获得较低的头值
如果a[0]合并_数组(a,b)
#=> [1, 3, 4, 5, 6, 8, 10, 11, 12, 14, 15, 19]
我得到一个错误“未定义的方法”`
我收到一个错误“undefined method”我对您的代码做了一些细微的更改以使其正常工作。见里面的评论

array_one = [2, 3, 4, 8, 10, 11, 12, 13, 15]
array_two = [1, 5, 6, 7, 9, 14]

def merge_arrays(array_one, array_two) 
  array_one, array_two = array_two, array_one if array_one.length > array_two.length # (1) swap arrays to make satement (3) work, need array_two always be the longest
  merged_array_size = array_one.length + array_two.length
  merged_array = []

  current_index_on_one = 0
  current_index_on_two = 0
  current_merged_index = 0

  for i in (0...merged_array_size-1) # (2) three points to avoid the error
    if (!array_one[current_index_on_one].nil? && array_one[current_index_on_one] < array_two[current_index_on_two]) # (3) check also if array_one is nil
      merged_array[current_merged_index] = array_one[current_index_on_one]
      current_index_on_one += 1
      current_merged_index += 1
    else
      merged_array[current_merged_index] = array_two[current_index_on_two]
      current_index_on_two += 1
      current_merged_index += 1
    end
  end
  merged_array[current_merged_index] = array_one[current_index_on_one] || array_two[current_index_on_two] # (4) add the missing element at the end of the loop, looks what happen if you comment out this line
  return merged_array
end

p merge_arrays(array_one, array_two)
# => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

为了使代码正常工作,我对您的代码做了一些细微的更改。见里面的评论

array_one = [2, 3, 4, 8, 10, 11, 12, 13, 15]
array_two = [1, 5, 6, 7, 9, 14]

def merge_arrays(array_one, array_two) 
  array_one, array_two = array_two, array_one if array_one.length > array_two.length # (1) swap arrays to make satement (3) work, need array_two always be the longest
  merged_array_size = array_one.length + array_two.length
  merged_array = []

  current_index_on_one = 0
  current_index_on_two = 0
  current_merged_index = 0

  for i in (0...merged_array_size-1) # (2) three points to avoid the error
    if (!array_one[current_index_on_one].nil? && array_one[current_index_on_one] < array_two[current_index_on_two]) # (3) check also if array_one is nil
      merged_array[current_merged_index] = array_one[current_index_on_one]
      current_index_on_one += 1
      current_merged_index += 1
    else
      merged_array[current_merged_index] = array_two[current_index_on_two]
      current_index_on_two += 1
      current_merged_index += 1
    end
  end
  merged_array[current_merged_index] = array_one[current_index_on_one] || array_two[current_index_on_two] # (4) add the missing element at the end of the loop, looks what happen if you comment out this line
  return merged_array
end

p merge_arrays(array_one, array_two)
# => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
arr1=[3,4,8,9,12]
arr2=[1,5,7,8,13]
arr=[arr1,arr2]
idx=[0,0]
(arr1.size+arr2.size).times.with_object([])do |(a|
imin=[0,1].min|u by{i|arr[i][idx[i]]| Float::INFINITY}
a[1,3,4,5,7,8,8,9,12,13]
arr1=[3,4,8,9,12]
arr2=[1,5,7,8,13]
arr=[arr1,arr2]
idx=[0,0]
(arr1.size+arr2.size).times.with_object([])do |(a|
imin=[0,1].min|u by{i|arr[i][idx[i]]| Float::INFINITY}
a[1,3,4,5,7,8,8,9,12,13]

假设两个阵列具有相同的长度是否安全@Op您是否尝试在每个for循环上打印输出,我的猜测是,在上一次迭代中,其中一个数组已经循环,结果是您将一个数字与一个nil值进行比较是否可以安全地假设这两个数组具有相同的长度@你是否尝试在每个for循环上打印输出,我猜在最后一次迭代中,数组中的一个已经循环,结果是你将一个数字与一个nil值进行比较我猜OP是在寻找一个O(n)算法。如果数组真的很短(如示例中所示),我就不会浪费精力来编写合并算法,但如果数组较长,可能值得付出努力。我猜OP正在寻找一个O(n)算法。如果数组真的很短(如示例中所示),我就不会浪费精力来编写合并算法,但如果数组较长,可能值得付出努力。我也想到了同样的事情,但预期的错误(参数错误)与OP报告的不匹配(无方法错误)。可能是OP的错误报告了错误的错误。@sawa它可能是
NoMethodError
ArgumentError
,具体取决于实际值。我也想到了同样的事情,但预期的错误(参数错误)与OP报告的不匹配(无方法错误)。可能是OP的错误报告了错误的错误。@sawa它可能是
NoMethodError
ArgumentError
,具体取决于实际值。
if 8 < nil
  # ...
end
array_one = [2, 3, 4, 8, 10, 11, 12, 13, 15]
array_two = [1, 5, 6, 7, 9, 14]

def merge_arrays(array_one, array_two) 
  array_one, array_two = array_two, array_one if array_one.length > array_two.length # (1) swap arrays to make satement (3) work, need array_two always be the longest
  merged_array_size = array_one.length + array_two.length
  merged_array = []

  current_index_on_one = 0
  current_index_on_two = 0
  current_merged_index = 0

  for i in (0...merged_array_size-1) # (2) three points to avoid the error
    if (!array_one[current_index_on_one].nil? && array_one[current_index_on_one] < array_two[current_index_on_two]) # (3) check also if array_one is nil
      merged_array[current_merged_index] = array_one[current_index_on_one]
      current_index_on_one += 1
      current_merged_index += 1
    else
      merged_array[current_merged_index] = array_two[current_index_on_two]
      current_index_on_two += 1
      current_merged_index += 1
    end
  end
  merged_array[current_merged_index] = array_one[current_index_on_one] || array_two[current_index_on_two] # (4) add the missing element at the end of the loop, looks what happen if you comment out this line
  return merged_array
end

p merge_arrays(array_one, array_two)
# => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
# for i in (1...merged_array_size)
# and
# for i in (1..merged_array_size-1)
# and
# (merged_array_size-1).times do
arr1 = [3, 4, 8,  9, 12]
arr2 = [1, 5, 7,  8, 13]

arr = [arr1, arr2]
idx = [0, 0]

(arr1.size + arr2.size).times.with_object([]) do |_,a|
  imin = [0, 1].min_by { |i| arr[i][idx[i]] || Float::INFINITY }
  a << arr[imin][idx[imin]]
  idx[imin] += 1
end
  #=> [1, 3, 4, 5, 7, 8, 8, 9, 12, 13]