Skip to content

Instantly share code, notes, and snippets.

@caiguanhao
Created August 26, 2016 09:56
Show Gist options
  • Save caiguanhao/c5f0d95771225983d7ede5924b55338c to your computer and use it in GitHub Desktop.
Save caiguanhao/c5f0d95771225983d7ede5924b55338c to your computer and use it in GitHub Desktop.
Questions
  1. 给出任意多个有 5 个元素的数组,如 [[10, 32, 43, 11, 4], [7, 36, 44, 9, 4], ...],数组各项合计为 100 , 如何根据和指定数组 [1, 32, 48, 17, 2] 的相似度进行排序?最好是计算每项的相似分值,然后按分值排序。
def random_distribution(size = 5)
  a = []
  (size - 1).times do |i|
    s = a.inject(&:+) || 0
    a << rand(100-s)
  end
  a[size - 1] = 100 - a.inject(&:+)
  a
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment