将数组元素分配到块中的有效方法

时间:2022-06-17 21:43:00

Given an array of length N, how I can I equally distribute the elements of the array into another array of arbitrary length?

给定一个长度为N的数组,我怎样才能将数组的元素均等地分配到另一个任意长度的数组中?

For instance, I have 3 items in an array and I'd like it distributed evenly across another array of 9 slots.

例如,我在一个数组中有3个项目,我希望它在另一个9个插槽的数组中均匀分布。

[1, 2, 3]

should result in (something close to)

应该导致(接近)

[[], [], [1], [], [], [2], [], [], [3]]

However, if I have 9 items to distribute to an array length of 2, it should result in

但是,如果我有9个项目要分配到2的数组长度,它应该导致

[[1,2,3,4], [5,6,7,8,9]]

Thanks!

谢谢!

NOTE: The position of the resulting array items could differ according to the algorithm, but the intent is to get some level of uniform distribution. In the first example, the 0th item could be [1]. In the second example, the 0th item could have [1,2,3,4,5].

注意:结果数组项的位置可能因算法而异,但目的是获得某种程度的均匀分布。在第一个例子中,第0个项可能是[1]。在第二个例子中,第0个项目可能有[1,2,3,4,5]。

2 个解决方案

#1


2  

Here's an easy way to do that:

这是一个简单的方法:

def distribute(arr, slots)
  n = arr.size
  a = Array.new(slots) { [] }
  arr.each_with_index { |e,i| a[i*slots/n] << e }
  a
end

distribute([1,2,3], 9)
  #=> [[1], [], [], [2], [], [], [3], [], []]
distribute([*(1..9)], 2)
  #=> [[1, 2, 3, 4, 5], [6, 7, 8, 9]]

You could change the distributions that result by modifying i*slots/n.

您可以通过修改i * slots / n来更改分配结果。

#2


1  

So there are two totally different use cases here, one where you have to build an array of length n, the other where you need to split into an array of length n.

所以这里有两个完全不同的用例,一个用于构建长度为n的数组,另一个用于需要分割为长度为n的数组。

This feels like a homework assignment but I don't really have enough off these two use cases to see a pattern (unless I'm missing something huge).

这感觉就像一个家庭作业,但我真的没有足够的这两个用例来看一个模式(除非我错过了一些巨大的东西)。

Test cases:

测试用例:

it 'splits on n vals' do
    arr = [1,2,3]
    expect(chunk(arr, 9)).to eq [[], [], [1], [], [], [2], [], [], [3]]
  end

  it 'splits on n vals' do
    arr = [1,2,3,4,5,6,7,8,9]
    expect(chunk(arr,2)).to eq [[1,2,3,4,5],[6,7,8,9]]
  end

Code:

码:

def chunk(arr, num)
  if num < arr.length
    return arr.each_slice( (arr.size/num.to_f).round).to_a
  end
  array = []
  len = arr.length
  (0..num).each do |i|
    if (i % len == 0) && i != 0
      array[i-1] = [arr.first]
      array[i] = []
      arr.shift
    else
      array[i] =  []
    end
  end
  array.pop
  array
end

#1


2  

Here's an easy way to do that:

这是一个简单的方法:

def distribute(arr, slots)
  n = arr.size
  a = Array.new(slots) { [] }
  arr.each_with_index { |e,i| a[i*slots/n] << e }
  a
end

distribute([1,2,3], 9)
  #=> [[1], [], [], [2], [], [], [3], [], []]
distribute([*(1..9)], 2)
  #=> [[1, 2, 3, 4, 5], [6, 7, 8, 9]]

You could change the distributions that result by modifying i*slots/n.

您可以通过修改i * slots / n来更改分配结果。

#2


1  

So there are two totally different use cases here, one where you have to build an array of length n, the other where you need to split into an array of length n.

所以这里有两个完全不同的用例,一个用于构建长度为n的数组,另一个用于需要分割为长度为n的数组。

This feels like a homework assignment but I don't really have enough off these two use cases to see a pattern (unless I'm missing something huge).

这感觉就像一个家庭作业,但我真的没有足够的这两个用例来看一个模式(除非我错过了一些巨大的东西)。

Test cases:

测试用例:

it 'splits on n vals' do
    arr = [1,2,3]
    expect(chunk(arr, 9)).to eq [[], [], [1], [], [], [2], [], [], [3]]
  end

  it 'splits on n vals' do
    arr = [1,2,3,4,5,6,7,8,9]
    expect(chunk(arr,2)).to eq [[1,2,3,4,5],[6,7,8,9]]
  end

Code:

码:

def chunk(arr, num)
  if num < arr.length
    return arr.each_slice( (arr.size/num.to_f).round).to_a
  end
  array = []
  len = arr.length
  (0..num).each do |i|
    if (i % len == 0) && i != 0
      array[i-1] = [arr.first]
      array[i] = []
      arr.shift
    else
      array[i] =  []
    end
  end
  array.pop
  array
end