Array#repeated_combination():repeated_combination()是一種Array類方法,它返回數組中元素長度n的所有重複組合,然後返回數組本身。
用法:Array.repeated_combination()
參數:數組
返回:數組中元素長度為n的所有重複組合,然後返回數組本身。f。
示例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]]
相關用法
- Ruby Array one?()用法及代碼示例
- Ruby Array pop()用法及代碼示例
- Ruby Array take()用法及代碼示例
- Scala Array &()用法及代碼示例
- Ruby Array max()用法及代碼示例
- Ruby Array min()用法及代碼示例
- Ruby Array zip()用法及代碼示例
- Ruby Array map()用法及代碼示例
- Ruby Array none?()用法及代碼示例
- Ruby Array to_h()用法及代碼示例
- Ruby Array shuffle!()用法及代碼示例
- Ruby Array class first()用法及代碼示例
- Ruby Array repeated_permutation()用法及代碼示例
注:本文由純淨天空篩選整理自mayank5326大神的英文原創作品 Ruby | Array repeated_combination() function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。