ruby array_Array.combination()方法和Ruby中的示例

ruby array

Ruby Array.combination()方法 (Ruby Array.combination() Method)

In the last article, we have seen how we can make use of the Array.collect method. Array.collect method is quite similar to Array.map methods because both the methods are used to create some modifications in the Array instance. We have also learnt the destructive and non-destructive version of the Array.collect method. In this article, we will see how we can implement Array.combination method? We will understand them with the help of its syntax and program code.

在上一篇文章中,我们看到了如何利用Array.collect方法Array.collect方法Array.map方法非常相似,因为这两种方法都用于在Array实例中创建一些修改。 我们还学习了Array.collect方法的破坏性和非破坏性版本。 在本文中,我们将看到如何实现Array.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 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实例本身。 此方法不能保证所产生元素的顺序。 使用块或数组调用此方法,并通过.to_a方法将结果转换为Array实例。

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

如果不提供任何块,则枚举器本身将返回。

Syntax:

句法:

    combination(n) { |c| block }

Argument(s):

参数:

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

此方法仅需要一个参数。 此参数确定Array实例的元素可能组合的数量。

Example 1:

范例1:

=begin
  Ruby program to demonstrate combination method
=end

# array
a = [1, 2, 3, 4]

print a.combination(1).to_a  
puts ""

print a.combination(2).to_a
puts ""  

print a.combination(3).to_a  
puts ""

print a.combination(4).to_a  
puts ""

print a.combination(0).to_a  
puts ""

print a.combination(5).to_a  

Output

输出量

[[1], [2], [3], [4]]
[[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]]
[[1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4]]
[[1, 2, 3, 4]]
[[]]
[]

Explanation:

说明:

In the above code, you can observe that this method is used to create combinations of the Array elements. Combinations are being created based on the argument passed inside the method. Everything was going fine until the argument 4 passes because there is the presence of 4 elements in the Array. When the number 0 is passed, it created an empty Array instance or you can say that an empty Array has been returned and when the number 5 is passed then an empty Array has been returned because there is not the presence of five elements in the Array instance.

在上面的代码中,您可以观察到此方法用于创建Array元素的组合。 根据在方法内部传递的参数创建组合。 一切都进行得很好,直到参数4通过为止,因为数组中存在4个元素。 传递数字0时,它创建了一个空的Array实例,或者可以说返回了一个空的Array;当传递数字5时,则返回了一个空的Array,因为该数组中不存在五个元素实例。

Example 2:

范例2:

=begin
  Ruby program to demonstrate combination method
=end

# array 
a = ["Sangita", "Babita", "Rashmi"]

print a.combination(1).to_a  
puts ""

print a.combination(2).to_a
puts ""  

print a.combination(3).to_a  
puts ""

print a.combination(4).to_a  
puts ""

print a.combination(0).to_a  
puts ""

print a.combination(5).to_a  

Output

输出量

[["Sangita"], ["Babita"], ["Rashmi"]]
[["Sangita", "Babita"], ["Sangita", "Rashmi"], ["Babita", "Rashmi"]]
[["Sangita", "Babita", "Rashmi"]]
[]
[[]]
[]

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 combinations.

在上面的示例中,您可以观察到该方法也适用于String Array实例。 此方法在组合元素后返回元素。

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

ruby array

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值