📜  红宝石 |数组重复组合()函数

📅  最后修改于: 2022-05-13 01:55:27.033000             🧑  作者: Mango

红宝石 |数组重复组合()函数

Array#repeated_combination() :repeated_combination()是一个 Array 类方法,它返回数组中元素长度为 n 的所有重复组合,然后返回数组本身。

示例 #1:

# Ruby code for repeated_combination() method
   
# declaring array
a = [18, 22, 33, nil, 5, 6]
   
# declaring array
b = [1, 4, 1, 1, 88, 9]
   
# declaring array
c = []
   
# repeated_combination method example
puts "repeated_combination() method form : 
      #{a.repeated_combination(2).to_a}\n\n"
   
puts "repeated_combination() method form : 
      #{b.repeated_combination(1).to_a}\n\n"
   
puts "repeated_combination() method form : 
      #{c.repeated_combination(4).to_a}\n\n"

输出 :

repeated_combination() method form : [[18, 18], [18, 22], [18, 33], [18, nil], [18, 5], [18, 6], [22, 22], [22, 33], [22, nil], [22, 5], [22, 6], [33, 33], [33, nil], [33, 5], [33, 6], [nil, nil], [nil, 5], [nil, 6], [5, 5], [5, 6], [6, 6]]

repeated_combination() method form : [[1], [4], [1], [1], [88], [9]]

repeated_combination() method form : []

示例 #2:

# Ruby code for repeated_combination() method
   
# declaring array
a = ["abc", "nil", "dog"]
   
# declaring array
c = [nil]
   
# declaring array
b = ["cow", nil, "dog"]
   
# repeated_combination method example
puts "repeated_combination() method form :
     #{a.repeated_combination(2).to_a}\n\n"
   
puts "repeated_combination() method form :
     #{b.repeated_combination(1).to_a}\n\n"
   
puts "repeated_combination() method form :
     #{c.repeated_combination(4).to_a}\n\n"

输出 :

repeated_combination() method form : [["abc", "abc"], ["abc", "nil"], ["abc", "dog"], ["nil", "nil"], ["nil", "dog"], ["dog", "dog"]]

repeated_combination() method form : [["cow"], [nil], ["dog"]]

repeated_combination() method form : [[nil, nil, nil, nil]]