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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值