日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

repeated_Ruby中带有示例的Array.repeated_combination()方法

發(fā)布時間:2025/3/11 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 repeated_Ruby中带有示例的Array.repeated_combination()方法 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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()方法 。 你們都必須認為該方法必須執(zhí)行與創(chuàng)建某些元素的組合有關(guān)的操作。 它并不像看起來那么簡單。 好吧,我們將在其余內(nèi)容中解決這個問題。 我們將嘗試借助語法并演示程序代碼來理解它。

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實例中獲取元素,并根據(jù)方法中傳遞的數(shù)量進行重復(fù)組合,然后返回Array實例本身。 此方法不能保證所產(chǎn)生元素的順序。 使用塊或Array調(diào)用此方法,并通過.to_a方法將結(jié)果轉(zhuǎn)換為Array實例。

If you do not provide any block then the enumerator is returned itself.

如果不提供任何塊,則枚舉器本身將返回。

Syntax:

句法:

array.repeated_combination(n) { |c| block }

Argument(s) required:

所需參數(shù):

This method only requires one argument. This argument decides the number of repeated combinations possible from the elements of Array instance.

此方法僅需要一個參數(shù)。 此參數(shù)確定Array實例的元素可能重復(fù)組合的數(shù)量。

Example 1:

范例1:

=beginRuby 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.

在上面的代碼中,您可以觀察到此方法用于創(chuàng)建Array元素的重復(fù)組合。 根據(jù)在方法內(nèi)部傳遞的參數(shù)創(chuàng)建重復(fù)的組合。 與組合方法不同,在方法內(nèi)部傳遞的參數(shù)不必小于或等于Array實例的長度,這里沒有任何強制,可以進行n個組合,并且n可以是長度的乘積。數(shù)組實例乘以方法中傳遞的整數(shù)。

Example 2:

范例2:

=beginRuby 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實例。 此方法在重復(fù)組合后返回元素。 可以通過將Array實例的長度乘以方法調(diào)用時在方法內(nèi)部傳遞的整數(shù)來預(yù)測組合的數(shù)量。

翻譯自: https://www.includehelp.com/ruby/array-repeated_combination-method-with-example.aspx

repeated

總結(jié)

以上是生活随笔為你收集整理的repeated_Ruby中带有示例的Array.repeated_combination()方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。