当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Ruby Array repeated_permutation()用法及代码示例


Array#repeated_permutation():repeated_permutation()是一个Array类方法,该方法返回数组元素长度n的所有重复排列,然后返回数组本身。

用法:Array.repeated_permutation()

参数:数组


返回:数组元素长度为n的所有重复排列,然后返回数组本身。

示例1:

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

输出:

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

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

repeated_permutation() method form : 
    []

示例2:

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

输出:

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

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

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



相关用法


注:本文由纯净天空筛选整理自mayank5326大神的英文原创作品 Ruby | Array repeated_permutation() function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。