处理'剩余'元素

时间:2014-08-31 23:54:14

标签: ruby

给定一个数组names(长度未知),创建4个工作组,没有工作组可以少于3个!除非数组中只包含一个或两个名称。

CODE

 def groups(source)
   Hash[ source.each_slice(4).map.with_index { |key, value| [ "Group: #{value+1}", key] } ]
 end

实施例

names10 = ["Aname", "Bname", "Cname", "Dname", "Ename", "Fname", "Gnames", "Hnames",
           "Inames", "Jnames"]

puts groups(names10)
 #=> {"Group: 1"=>["Aname", "Bname", "Cname", "Dname"], "Group: 2"=>["Ename", "Fname",
 #    "Gnames", "Hnames"], "Group: 3"=>["Inames", "Jnames"]}

从上面的示例中可以看出,我的代码创建了一个具有两个名称的组3。期望的回报是取第3组中的名称并将它们分配到第1组和第2组。

预期回报

puts groups(names10)
 #=> {"Group: 1"=>["Aname", "Bname", "Cname", "Dname", "Jnames",], "Group: 2"=>["Ename",
 #    "Fname", "Gnames", "Hnames", "Jnames"]}

如果数组包含26个名称,我的代码将返回6组4和1组2,我希望能够在组7中取两个名称并在第5组和第6组之间分配。

我希望这能澄清我的问题。是的,我之前的代码被打破了!遗憾

1 个答案:

答案 0 :(得分:1)

我不确定我理解你的问题,但我会回答以下问题:

&#34;给定一个数组a和一个最小子阵列大小m1 < m <= a.size,返回一个数组b = [b1, b2,.., bf],用于对a的元素进行分区1}}到子数组bi, i=1..f,以便子数组的大小s,其中s >= ms尽可能小。&#34;

..然后我将返回的数组转换为您请求的哈希值。

<强>代码

def equal_partition(arr,min_sub_size)
  as = arr.size
  sz = (min_sub_size..as).find { |sub_size| (as % sub_size).zero? }
  arr.each_slice(sz).map.with_index(1) { |a,i| ["Group: #{i}:", a] }.to_h
end

<强>实施例

names10 = ["Aname", "Bname",  "Cname",  "Dname",  "Ename",
           "Fname", "Gnames", "Hnames", "Inames", "Jnames"]

equal_partition(names10, 3)
  #=> {"Group: 1:"=>["Aname", "Bname",  "Cname",  "Dname",  "Ename"],
  #    "Group: 2:"=>["Fname", "Gnames", "Hnames", "Inames", "Jnames"]}
equal_partition(names10, 2)
  #=> {"Group: 1:"=>["Aname", "Bname"], "Group: 2:"=>["Cname",  "Dname"],
  #    "Group: 3:"=>["Ename", "Fname"], "Group: 4:"=>["Gnames", "Hnames"],
  #    "Group: 5:"=>["Inames", "Jnames"]}
equal_partition(names10, 6)
  #=> {"Group: 1:"=>["Aname", "Bname",  "Cname",  "Dname",  "Ename",
  #                  "Fname", "Gnames", "Hnames", "Inames", "Jnames"]}
equal_partition(names10, 1)
  #=> {"Group: 1:"=>["Aname"],  "Group: 2:"=> ["Bname"],
  #    "Group: 3:"=>["Cname"],  "Group: 4:"=> ["Dname"],
  #    "Group: 5:"=>["Ename"],  "Group: 6:"=> ["Fname"],
  #    "Group: 7:"=>["Gnames"], "Group: 8:"=> ["Hnames"],
  #    "Group: 9:"=>["Inames"], "Group: 10:"=>["Jnames"]}

names12 = ["Aname", "Bname",   "Cname",  "Dname",  "Ename", "Fname",
           "Gnames", "Hnames", "Inames", "Jnames", "Kname", "Lname"]

equal_partition(names12, 3)
  #=> {"Group: 1:"=>["Aname",  "Bname",  "Cname"],
  #    "Group: 2:"=>["Dname",  "Ename",  "Fname"],
  #    "Group: 3:"=>["Gnames", "Hnames", "Inames"],
  #    "Group: 4:"=>["Jnames", "Kname",  "Lname"]}

equal_partition(names12, 4)
  #=> {"Group: 1:"=>["Aname",  "Bname",  "Cname",  "Dname"],
  #    "Group: 2:"=>["Ename",  "Fname",  "Gnames", "Hnames"],
  #    "Group: 3:"=>["Inames", "Jnames", "Kname",  "Lname"]}

equal_partition(names12, 5)
  #=> {"Group: 1:"=>["Aname",  "Bname",  "Cname",  "Dname",  "Ename", "Fname"],
  #    "Group: 2:"=>["Gnames", "Hnames", "Inames", "Jnames", "Kname", "Lname"]}

equal_partition(names12, 6)
  #=> {"Group: 1:"=>["Aname",  "Bname",  "Cname",  "Dname",  "Ename", "Fname"],
  #    "Group: 2:"=>["Gnames", "Hnames", "Inames", "Jnames", "Kname", "Lname"]}

<强>解释

假设我们希望计算:

equal_partition(names10, 3)

然后

arr = names10
min_sub_size = 3
as = arr.size

sz = (min_sub_size..as).find { |sub_size| (as % sub_size).zero? }
  #=> (3..10).find { |sub_size| (10 % sub_size).zero? }
  #=> 5

enum1 = arr.each_slice(5)
  #=> #<Enumerator: ["Aname",  "Bname",  "Cname",  "Dname", "Ename",
  #         "Fname", "Gnames", "Hnames", "Inames", "Jnames"]:each_slice(5)>
enum2 = enum1.map
  #=> #<Enumerator: #<Enumerator: ["Aname", "Bname", "Cname", "Dname",
  #         "Ename", "Fname", "Gnames", "Hnames", "Inames",
  #         "Jnames"]:each_slice(5)>:map>
enum3 = enum2.with_index(1)
  #=> #<Enumerator: #<Enumerator: #<Enumerator: ["Aname", "Bname",
  #         "Cname", "Dname", "Ename", "Fname", "Gnames", "Hnames",
  #         "Inames", "Jnames"]:each_slice(5)>:map>:with_index(1)>

enum2enum3可以被视为&#34;复合&#34;统计员。我们可以将enum3转换为数组,以查看它将传递到其块中的(两个)对象:

enum3.to_a
  #=> [[["Aname", "Bname",  "Cname",  "Dname",  "Ename"],  1],
  #    [["Fname", "Gnames", "Hnames", "Inames", "Jnames"], 2]]

a = enum3.each { |a,i| ["Group: #{i}:", a] }
  #=> [["Group: 1:", ["Aname", "Bname",  "Cname",  "Dname",  "Ename"]],
  #    ["Group: 2:", ["Fname", "Gnames", "Hnames", "Inames", "Jnames"]]]

a.to_h
  #=> {"Group: 1:"=>["Aname", "Bname",  "Cname",   "Dname", "Ename"],
  #    "Group: 2:"=>["Fname", "Gnames", "Hnames", "Inames", "Jnames"]}

对于2.0之前的Ruby版本,最后一步必须替换为:

Hash[a]