gpt4 book ai didi

ruby - 将数组拆分为新数组(每个数组都有唯一的名称)

转载 作者:太空宇宙 更新时间:2023-11-03 17:48:22 26 4
gpt4 key购买 nike

<分区>

我正在尝试将数组分割成相等大小(向下舍入)并将每个部分保存到相应的变量中。

each_slice 方法已用于抓取 n 个大小的 block 。但是我想不出办法:

  1. 遍历每个 block 的“子索引”
  2. 为每个创建一个新数组并给每个一个唯一的名称。

    letters = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n"]

    def groups_of_five(array)
    split_array = array.each_slice(5).to_a

    #something like the following:
    #array(n) = Array[split_array.each {|x| x}]

    end
    end

我希望的输出:

groups_of_five(letters)
=> array1: ["a,"b","c","d","e"]
=> array2: ["f","g","h","i","j"]
=> array3: ["k","l","m","n"]

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