如何将字符串元素分组为所需的长度

时间:2015-07-16 22:43:18

标签: ruby algorithm math mathematical-optimization

我想创建一个字符串数据集的子集。例如,我有150个不同长度的单词。我需要将它们分发给最大长度为100字符的组。我找不到以最佳方式做到这一点的任何方法。对于这种情况,最小数量的新组。

由于我不熟悉堆栈的一般格式,对不起复杂的问题。

网站数组是我的数据样本,完整的数据只包含更多网站。我想要做的是将这些网站放到具有255个长度限制的新阵列。我想通过创建最少数量的新阵列,以最有效的方式做到这一点。

websites=["0n-line.tv","100dollars-seo.com","12masterov.com","1pamm.ru","4webmasters.org","5forex.ru","7makemoneyonline.com","acads.net"]

websiteslength =[10, 18, 14, 8, 15, 9, 20, 9, 10, 11]

这是我到目前为止编写的代码,但它并没有完全符合我的要求。

while total <= 255
  puts total
  total += websiteslength[y]
  total += 1
  if total <= 255
    y += 1
  else
    total -= a[y]
    y += 1
    total += a[y]
    if total == 255
      #total has reached to 255 start new sub-array
    else
      total -= a[y]
      y += 1
      total += a[y]
      if total == 255 
      #total has reached to 255 start new sub-array
      else
      total -= a[y]
      y += 1
      total += a[y]
      end
    end
  end
end

0 个答案:

没有答案