这个问题在其他地方被问过,但我只是想检查一下我做了什么是适用于rspec的情况:
编写一个方法,该方法接受两个排序的数组并生成组合两个数组的排序数组。
限制:
提示:您可能需要索引到两个数组中。
combine_arrays([1, 3, 5], [2, 4, 6]) == [1, 2, 3, 4, 5, 6]
您可以将两个数组合并为一个数组,然后运行典型的冒泡排序吗?
def combine_arrays(arr1,arr2)
final = arr1 + arr2
sorted = true
while sorted do
sorted = false
(0..final.length - 2).each do |x|
if final[x] > final[x+1]
final[x], final[x+1] = final[x+1], final[x]
sorted = true
end
end
end
final
end
p combine_arrays([1,3,5],[2,4,6]) => [1, 2, 3, 4, 5, 6]
答案 0 :(得分:1)
这是一个完全依赖于Ruby的枚举器的变体。结果很短而且很甜蜜。
# merge two sorted arrays into a sorted combined array
def merge(a1, a2)
[].tap do |combined|
e1, e2 = a1.each, a2.each
# The following three loops terminate appropriately because
# StopIteration acts as a break for Kernel#loop.
# First, keep appending smaller element until one of
# the enumerators run out of data
loop { combined << (e1.peek <= e2.peek ? e1 : e2).next }
# At this point, one of these enumerators is "empty" and will
# break immediately. The other appends all remaining data.
loop { combined << e1.next }
loop { combined << e2.next }
end
end
第一个loop
继续抓取两个枚举器值中的最小值,直到其中一个枚举器用完了值。第二个loop
然后从第一个数组的枚举器中追加所有剩余的(可能是无)值,第三个loop
对第二个数组的枚举器执行相同的操作,tap
将结果返回给结果阵列。
答案 1 :(得分:0)
当然,你可以做到这一点,但你忽视了一个真正的噱头 - 你给出的两个数组已经被分类了。
def combine_arrays(A1, A2)
retVal = Array.CreateInstance(System::Int32, A1.Length + A2.Length - 1)
i = 0
j = 0
while i < A1.Length | j < A2.Length
if i < A1.Length and self.A1(i) < self.A2(j) then
self.retVal(i + j) = self.A1(i)
i += 1
else
self.retVal(i + j) = self.A2(j)
j += 1
end
end
return retVal
end
答案 2 :(得分:0)
这是基于与Dale M的帖子相同的逻辑,但是在适当的红宝石中:
def combine_arrays(arr1,arr2)
[].tap do |out|
i1 = i2 = 0
while i1 < arr1.size || i2 < arr2.size
v1 = arr1[i1]
v2 = arr2[i2]
if v1 && (!v2 || v1 < v2)
out << v1
i1 += 1
else
out << v2
i2 += 1
end
end
end
end
combine_arrays([1,3,5], [2,4,6])
答案 3 :(得分:0)
看一下这个:
def merge(arr1, arr2)
arr2.each { |n| arr1 = insert_into_place(arr1, n) }
arr1.empty? ? arr2 : arr1
end
def insert_into_place(array, number)
return [] if array.empty?
group = array.group_by { |n| n >= number }
bigger = group[true]
smaller = group[false]
if bigger.nil?
number > smaller.last ? smaller << number : smaller.unshift(number)
else
(smaller << number) + bigger
end
end