gpt4 book ai didi

arrays - 按子数组总和大小拆分子数组 Ruby

转载 作者:行者123 更新时间:2023-12-04 01:16:03 25 4
gpt4 key购买 nike

我有一个子数组:

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 个子数组,我的代码不会以这种形式拆分它。

最佳答案

您可以使用 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 方法会像 eachmap 一样,通过遍历可枚举对象的每个元素,将任何可枚举对象“缩减”为单个值

考虑一个例子:

# 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

所以在这种情况下,在每个循环中,我们将项目的值添加到累加器并返回累加器以供下一个循环使用。一旦 reduce 迭代了数组中的所有项目,它将返回累加器。

Ruby 还提供语法糖使其看起来更漂亮:

arr.reduce(:+) # return 6

这是一个很好的article进一步引用

因此,如果您以您的问题为例:

# 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

关于arrays - 按子数组总和大小拆分子数组 Ruby,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63346335/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com