repeated
Array.repeated_combination()方法 (Array.repeated_combination() Method)
In this article, we will study about Array.repeated_combination() method. You all must be thinking the method must be doing something which is related to creating combinations of certain elements. It is not as simple as it looks. Well, we will 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_combination()方法 。 你們都必須認為該方法必須執行與創建某些元素的組合有關的操作。 它并不像看起來那么簡單。 好吧,我們將在其余內容中解決這個問題。 我們將嘗試借助語法并演示程序代碼來理解它。
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 combinations 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_combination(n) { |c| block }
Argument(s) required:
所需參數:
This method only requires one argument. This argument decides the number of repeated combinations possible from the elements of Array instance.
此方法僅需要一個參數。 此參數確定Array實例的元素可能重復組合的數量。
Example 1:
范例1:
=begin
Ruby program to demonstrate repeated_combination method
=end
# array declaration
a = [1, 2, 3]
print a.repeated_combination(1).to_a
puts ""
print a.repeated_combination(2).to_a
puts ""
print a.repeated_combination(3).to_a
Output
輸出量
[[1], [2], [3]]
[[1, 1], [1, 2], [1, 3], [2, 2], [2, 3], [3, 3]]
[[1, 1, 1], [1, 1, 2], [1, 1, 3], [1, 2, 2], [1, 2, 3], [1, 3, 3], [2, 2, 2], [2, 2, 3], [2, 3, 3], [3, 3, 3]]
Explanation:
說明:
In the above code, you can observe that this method is used to create repeated combinations of the Array elements. Repeated combinations are being created on the basis of the argument passed inside the method. Unlike combination method it is not necessary that the argument passed inside the method should be less than or equal to the length of Array instance, here is no any compulsion, n number of combinations can be made and that n can be the multiplication of length of Array instance multiplied by the integer passed in the method.
在上面的代碼中,您可以觀察到此方法用于創建Array元素的重復組合。 根據在方法內部傳遞的參數創建重復的組合。 與組合方法不同,在方法內部傳遞的參數不必小于或等于Array實例的長度,這里沒有任何強制,可以進行n個組合,并且n可以是長度的乘積。數組實例乘以方法中傳遞的整數。
Example 2:
范例2:
=begin
Ruby program to demonstrate repeated_combination method
=end
# array declaration
a = ["Sangita", "Babita"]
print a.repeated_combination(1).to_a
puts ""
print a.repeated_combination(2).to_a
puts ""
print a.repeated_combination(3).to_a
Output
輸出量
[["Sangita"], ["Babita"]]
[["Sangita", "Sangita"], ["Sangita", "Babita"], ["Babita", "Babita"]]
[["Sangita", "Sangita", "Sangita"], ["Sangita", "Sangita", "Babita"], ["Sangita", "Babita", "Babita"], ["Babita", "Babita", "Babita"]]
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 combinations. The number of combinations can be predicted by multiplying the length of Array instance with the integer passed inside the method at the time of method invocation.
在上面的示例中,您可以觀察到該方法也適用于String Array實例。 此方法在重復組合后返回元素。 可以通過將Array實例的長度乘以方法調用時在方法內部傳遞的整數來預測組合的數量。
翻譯自: https://www.includehelp.com/ruby/array-repeated_combination-method-with-example.aspx
repeated