我有一个子阵列:
arr = [["a", "b", "c"], ["a", "b"], ["a", "b", "c"], ["a", "c"],
["c", "v"], ["c", "f"], ["e", "a"], ["a", "b", "v"],
["a", "n", "c"], ["a", "b", "m"], ["a", "c"], ["a", "c", "g"]]
我想将每个子数组的元素放入另一个数组中,但子数组大小的总和必须小于或等于6。所以我想得到这样的东西
[["a", "b", "c", "a", "b"], ["a", "b", "c", "a", "c"],
["c", "v", "c", "f", "e", "a"], ["a", "b", "v", "a", "n", "c"],
["a", "b", "m", "a", "c"], ["a", "c", "g"]]
我现在的代码是
stop = 0
new_arr = []
indexo = ""
arr.each_with_index do |x, index|
stop = stop + x.size
if stop <= 6
new_arr << x
indexo = index
end
end
我被困在这里,因为我的代码只有两个前元素。原始数组有大约1000个子数组,我的代码没有以那种形式分割它。
arr = [["a", "b", "c"], ["a", "b"], ["a", "b", "c"], ["a", "c"],
["c", "v"], ["c", "f"], ["e", "a"], ["a", "b", "v"],
["a", "n", "c"], ["a", "b", "m"], ["a", "c"], ["a", "c", "g"]]
arr.each_with_object([[]]) do |a,ar|
if a.size + ar[-1].size > 6
ar << a
else
ar[-1] += a
end
end
#=> [["a", "b", "c", "a", "b"], ["a", "b", "c", "a", "c"],
# ["c", "v", "c", "f", "e", "a"], ["a", "b", "v", "a", "n", "c"],
# ["a", "b", "m", "a", "c"], ["a", "c", "g"]]
步骤如下。
enum = arr.each_with_object([[]])
#=> #<Enumerator: [["a", "b", "c", "a", "b"], ["a", "b"],...
# ["a", "c", "g"]]:each_with_object([[]])>
第一个值由此枚举器生成,传递给块,块值通过对传递给块的双元素数组应用 Array 分解来分配值。
a, ar = enum.next
#=> [["a", "b", "c"], [[]]]
a #=> ["a", "b", "c"]
ar #=> [[]]
请参见下面的枚举数#。然后计算条件语句。
a.size + ar[-1].size > 6
#=> 3 + 0 > 6 => false
因此我们执行:
ar[-1] += a
#=> ["a", "b", "c"]
ar #=> [["a", "b", "c"]]
下一个元素由enum
生成,传递给块,块值为赋值。
a, ar = enum.next
#=> [["a", "b"], [["a", "b", "c"]]]
a #=> ["a", "b"]
ar #=> [["a", "b", "c"]]
计算条件语句。
a.size + ar[-1].size > 6
#=> 2 + 3 > 6 => false
所以我们再次执行:
ar[-1] += a
#=> ["a", "b", "c", "a", "b"]
ar #=> [["a", "b", "c", "a", "b"]]
< code>enum然后将第三个元素传递给块。
a, ar = enum.next
#=> [["a", "b", "c"], [["a", "b", "c", "a", "b"]]]
a #=> ["a", "b", "c"]
ar #=> [["a", "b", "c", "a", "b"]]
因为:
a.size + ar[-1].size > 6
#=> 3 + 5 > 6 => false
这次我们执行
ar << a
#=> [["a", "b", "c", "a", "b"], ["a", "b", "c"]]
其余步骤类似。
您可以使用reduce方法,并不断将子数组推送到一个新数组中。请考虑以下情况:
new_arr = arr.reduce([]) do |acc, sub_array|
last_element = acc[acc.length - 1]
if last_element.nil? or (last_element + sub_array).length > 6
acc << sub_array
else
acc[acc.length - 1] = last_element + sub_array
end
acc
end
# Tests
new_arr.flatten.size == arr.flatten.size # test total number of elements in both the arrays
new_arr.map(&:size) # the sizes of all sub arrays
new_arr.map(&:size).min # min size of all sub arrays
new_arr.map(&:size).max # max size of all sub arrays
如果你不清楚密码就告诉我
更新:
Reduce 方法将通过迭代可枚举的每个元素将任何可枚举对象“减少”为单个值,就像每个
映射
一样
考虑一个例子:
# Find the sum of array
arr = [1, 2, 3]
# Reduce will accept an initial value & a block with two arguments
# initial_value: is used to set the value of the accumulator in the first loop
# Block Arguments:
# accumulator: accumulates data through the loop and finally returned by :reduce
# value: each item of the above array in every loop(just like :each)
arr.reduce(0) do |acc, value|
# initial value is 0; in the first loop acc's value will be set to 0
# henceforth acc's value will be what is returned from the block in every loop
acc += value
acc # acc is begin returned; in the second loop the value of acc will be (0 + 1)
end
因此,在这种情况下,在每个循环中,我们将项目的值添加到累加器并返回累加器以在下一个循环中使用。一旦减少迭代了数组中的所有项目,它将返回累加器。
Ruby还提供了语法糖,使其看起来更花哨:
arr.reduce(:+) # return 6
这是一篇好文章,供进一步参考
因此,如果您以您的问题为例:
# Initial value is set to an empty array, what we're passing to reduce
new_arr = arr.reduce([]) do |acc, sub_array|
# In the first loop acc's value will be set to []
# we're finding the last element of acc (in first loop since the array is empty
# last element will be nil)
last_element = acc[acc.length - 1]
# If last_element is nil(in first loop) we push the first item of the array to acc
# If last_element is found(pushed in the previous loops), we take it and sum
# it with the item from the current loop and see the size, if size is more
# than 6, we only push the item from current loop
if last_element.nil? or (last_element + sub_array).length > 6
acc << sub_array
else
# If last element is present & last_element + item from current loop's size
# is less than 6, we push the (last_element + item from current loop) into
# the accumulator.
acc[acc.length - 1] = last_element + sub_array
end
# Finally we return the accumulator, which will be used in the next loop
# Or if has looped through the entire array, it will be used to return back
# from where it was called
acc
end
基本上,我要问的是给定一个正方形2D阵列和一个有效的补丁大小(2D子阵列的大小),我将如何做到这一点。最终,我不需要以任何方式存储子阵列,我只需要找到每个子阵列的中值并将它们存储在一个一维阵列中。中值和存储到新阵列对我来说很简单,我只是不知道如何处理原始2D阵列并正确拆分它。我已经尝试了几次,但一直出现越界错误。我有一个4x4: 我需要像这样拆分它 < code>[1,2] [3,4] [2,3]
问题内容: 假设我们有一个整数数组:a = {2,4,3,5} 我们有k = 3。 我们可以将数组a拆分为k(3)个子数组,其中数组的顺序无法更改。每个子阵列的总和必须尽可能低,以使所有子阵列之间的最大总和尽可能低。 对于上述解决方案,这将给出{2,4},{3},{5},其最大和为6(4 + 2)。 错误的答案将是{2},{4、3},{5},因为在这种情况下,最大总和为7(4 + 3)。 我尝试创
给定一个值数组,我如何将它分成由相等元素组成的? 给定这个数组 我想要这个输出 解决这一问题的一种可能方法是创建某种索引,以指示每个元素的出现情况。 最后使用索引重建输出数组。 但是,使用此解决方案,我会丢失原始值。当然,在这种情况下,这不是一个大问题(一个值仍然存在,即使重新创建,),但我想将此解决方案应用于像这样更复杂的数据结构 实际上,我正在寻找的函数是与相反的函数 我希望我已经说清楚了,如
对于使用分治方法的最大和子数组算法,我需要返回和和子数组。 我能在所有的测试中正确地计算和。然而,我无法计算出正确的子数组。 我的总数(正确):34 阵列:2 9 8 6 5-11 9-11 7 5-1-8-3 7-2 正确子数组:2 9 8 6 5 我的总数(正确):50 阵列:31-41 59 26-53 58 97-93-23 84 正确子数组:59 26-53 58 97 我的总数(正确)
我试图在一个数组中找到具有最大和的邻接子数组。所以,对于数组 {5,15,-30,10,-5,40,10}
很抱歉发了这么长的帖子,但我不明白我做错了什么。感谢您事先的帮助! 这里是当前面提到的列表作为输入给出时的输出,我已经经历并查看了每一个步骤,列表中的每一个消除在我看来都是合乎逻辑的,我不知道一个人怎么会以结束和105结束。如果有人能帮我理解,我会非常感激的!