repeated
Array.repeated_permutation()方法 (Array.repeated_permutation() Method)
In this article, we will study about Array.repeated_permutation() method. You all must be thinking the method must be doing something which is related to creating permutations of certain elements. Let's figure this out in the rest of our content. We will try to understand it with the help of syntax and demonstrating program codes.
在本文中,我们将研究Array.repeated_permutation()方法 。 大家都必须认为该方法必须执行与创建某些元素的排列有关的操作。 让我们在其余内容中弄清楚这一点。 我们将尝试借助语法并演示程序代码来理解它。
Method description:
方法说明:
This method is a public instance method and defined for the Array class in Ruby's library. This method works in a way that it will take elements from the Array instance and make repeated permutations according to the number passed in the method and then it returns the Array instance itself. This method does not guarantee the order of the elements yielded. This method is invoked with a block or an Array and the result is being converted into the Array instance with the help of .to_a method.
该方法是一个公共实例方法,为Ruby库中的Array类定义。 此方法的工作方式是从Array实例中获取元素,并根据方法中传递的数字进行重复排列,然后返回Array实例本身。 此方法不能保证所产生元素的顺序。 使用块或Array调用此方法,并通过.to_a方法将结果转换为Array实例。
If you do not provide any block then the enumerator is returned itself.
如果不提供任何块,则枚举器本身将返回。
Syntax:
句法:
array.repeated_permutation(n) { |c| block }
Argument(s) required:
所需参数:
This method only requires one argument. This argument decides the number of repeated permutations possible from the elements of Array instance.
此方法仅需要一个参数。 此参数确定Array实例的元素可能重复排列的次数。
Example 1:
范例1:
=begin
Ruby program to demonstrate repeated_permutation method
=end
# array declaration
a = [1, 2]
print a.repeated_permutation(1).to_a
puts ""
print a.repeated_permutation(2).to_a
puts ""
print a.repeated_permutation(3).to_a
Output
输出量
[[1], [2]]
[[1, 1], [1, 2], [2, 1], [2, 2]]
[[1, 1, 1], [1, 1, 2], [1, 2, 1], [1, 2, 2], [2, 1, 1], [2, 1, 2], [2, 2, 1], [2, 2, 2]]
Explanation:
说明:
In the above code, you can observe that this method is used to create repeated permutations of the array elements. Repeated permutations are being created on the basis of the argument passed inside the method. You can predict the number of permutations that can be generated by keeping the length of the Array as the base and the integer passed as the argument as power. In the above code, you can observe that when the integer 3 is passed, 8 permutations are created.
在上面的代码中,您可以观察到此方法用于创建数组元素的重复排列。 根据在方法内部传递的参数创建重复的排列。 您可以通过将Array的长度作为基数并将整数作为参数传递为幂来预测可以生成的排列数。 在上面的代码中,您可以观察到,当传递整数3时,将创建8个排列。
Example 2:
范例2:
=begin
Ruby program to demonstrate repeated_permutation method
=end
# array declaration
a = ["Kamiara","Arshi"]
print a.repeated_permutation(1).to_a
puts ""
print a.repeated_permutation(2).to_a
puts ""
print a.repeated_permutation(3).to_a
Output
输出量
[["Kamiara"], ["Arshi"]]
[["Kamiara", "Kamiara"], ["Kamiara", "Arshi"], ["Arshi", "Kamiara"], ["Arshi", "Arshi"]]
[["Kamiara", "Kamiara", "Kamiara"], ["Kamiara", "Kamiara", "Arshi"], ["Kamiara", "Arshi", "Kamiara"], ["Kamiara", "Arshi", "Arshi"], ["Arshi", "Kamiara", "Kamiara"], ["Arshi", "Kamiara", "Arshi"], ["Arshi", "Arshi", "Kamiara"], ["Arshi", "Arshi", "Arshi"]]
Explanation:
说明:
In the above example you can observe that this method works upon String Array instances as well. This method is returning elements after making their repeated permutations. You can predict the number of permutations that can be generated by keep the length of the Array as the base and the integer passed as the argument as power. In the above code, you can observe that when the integer 3 is passed, 8 permutations are created.
在上面的示例中,您可以观察到该方法也适用于String Array实例。 此方法在重复排列后返回元素。 您可以通过将Array的长度作为基数并将整数作为参数传递为幂来预测可以生成的排列数。 在上面的代码中,您可以观察到,当传递整数3时,将创建8个排列。
翻译自: https://www.includehelp.com/ruby/array-repeated_permutation-method-with-example.aspx
repeated