ruby 集合 分组
For every problem, we can identify different alternatives after identifying them. For this problem too, we have different ways to check whether the Set is empty or not. Ruby has given us various methods through which we can perform multiple tasks. Some can be used directly for solving the purpose and some we can use indirectly also. This depends upon us that how we make an effective utilisation of the method we have and how effectively our code is written.
对于每个问题,我们都可以在识别出不同的替代方案后加以识别。 对于这个问题,我们也有不同的方法来检查Set是否为空 。 Ruby为我们提供了多种方法,通过它们可以执行多个任务。 有些可以直接用于解决目的,有些也可以间接使用。 这取决于我们如何有效利用已有的方法以及代码的编写效率。
Methods used:
使用的方法:
Set.merge(): This method is used to merge two sets.
Set.merge() :此方法用于合并两个集合。
Set.empty?(): This method is used to check whether the set is empty or not. The return type of this method is Boolean.
Set.empty?() :此方法用于检查集合是否为空。 此方法的返回类型为Boolean。
Set.size(): This method returns the size of the set.
Set.size() :此方法返回集合的大小。
Set.each: This method is used to traverse the elements of the set.
Set.each :此方法用于遍历集合中的元素。
Variables used:
使用的变量:
Vegetable: This is an instance of Set class.
Vegetable :这是Set类的实例。
Fruits: This is an instance of Set class.
水果 :这是Set类的实例。
count: This is a counter variable which is used to count the number of elements present in the set.
count :这是一个计数器变量,用于计算集合中存在的元素数。
Program 1:
程序1:
=begin
Ruby program to find the empty set
with Set.empty?() method.
=end
require 'set'
Vegetable = Set.new(["potato","brocolli","broccoflower","lentils","peas","fennel","chilli","cabbage"])
Fruits = Set.new(["Apple","Mango","Banana","Orange","Grapes"])
Vegetable.merge(Fruits)
if Vegetable.empty?()
puts "Set is empty"
else
puts "Set is not empty"
end
Output
输出量
Set is not empty
Explanation:
说明:
In the above code, we are making use of Set.empty?() method which is predefined in Ruby's library. This makes the task very easy because we only have to write a single line and out task is done. The output tells that the set is not empty because set is having elements which we have added at the time of its definition as well as after merging it with another set.
在上面的代码中,我们使用了Ruby库中预定义的Set.empty?()方法 。 这使任务非常容易,因为我们只需要写一行就可以完成任务。 输出表明该集不为空,因为该集包含在定义它时以及与另一个集合并后添加的元素。
Program 2:
程式2:
=begin
Ruby program to find the empty set.
=end
require 'set'
Vegetable = Set.new([ "potato", "brocolli" , "broccoflower" , "lentils" , "peas" , "fennel" , "chilli" , "cabbage" ])
Fruits = Set.new(["Apple","Mango","Banana","Orange","Grapes"])
Vegetable.merge(Fruits)
count = 0
Vegetable.each do |element|
count +=1
end
if count == 0
puts "Set is empty"
else
puts "Set is not empty"
end
Output
输出量
Set is not empty
Explanation:
说明:
In the above code, we are using Set.each to check whether set is empty or not. We are using a counter variable which is getting incremented inside the loop. If the counter variable remains 0 after the successful termination of loop, this means that the set contains nothing as element. We will find that the set is not empty because we have added elements to it at the time of definition.
在上面的代码中,我们使用Set.each来检查set是否为空 。 我们正在使用一个计数器变量,该变量在循环内部递增。 如果成功终止循环后,计数器变量仍为0,则意味着该集合不包含任何元素。 我们将发现集合不是空的,因为在定义时已向其中添加了元素。
Program 3:
程式3:
=begin
Ruby program to find the empty set.
=end
require 'set'
Vegetable = Set.new([ "potato" , "brocolli" , "broccoflower" , "lentils" , "peas" , "fennel" , "chilli" , "cabbage" ])
Fruits = Set.new(["Apple","Mango","Banana","Orange","Grapes"])
Vegetable.merge(Fruits)
if Vegetable.size() == 0
puts "Set is empty"
else
puts "Set is not empty"
end
Output
输出量
Set is not empty
Explanation:
说明:
In the above code, we are using Set.size() method to check whether the set is empty or not. It is very obvious that if the size of set is 0 then it is empty otherwise not.
在上面的代码中,我们使用Set.size()方法 检查集合是否为空 。 很明显,如果set的大小为0,则为空,否则为空。
翻译自: https://www.includehelp.com/ruby/check-whether-the-set-is-empty-or-not.aspx
ruby 集合 分组