是否可以确定数组是否包含特定值和其他值?
array = [1, 1, 3, 4]
array.include? 1 #=> true
我可以使用include?
来确定它是否包含数字1
,但在此之后,如何检查它是否包含1
以外的值,而不是关心这些值是什么?
答案 0 :(得分:2)
您可以将元素数量与1
的出现次数进行比较:
array = [1, 1, 3, 4]
p array.count(1) == array.size #true if only 1 are in array (or empty)
p array.count(1) != array.size #True if other values are available,
答案 1 :(得分:2)
可能有很多方法可以更好地回答这个问题,但如果不了解你如何使用这些信息的更广泛背景,很难给出准确的答案。
话虽如此,你有很多选择。我不确定你可以做一个单独的操作来测试它,但是一个聪明的解决方案可能是这样的:
array.chunk {|v| v == 1 }.to_a.length == 2
这样做会返回一个块结果数组和与这些块结果匹配的值。如果该数组的长度为2,那么您就知道该数组的两个值都匹配且不匹配1
。
这是Θ(n);您可以使用多段代码实现更快的解决方案,例如:
array.include?(1) && array.any? {|v| v != 1}
答案 2 :(得分:1)
您可以将方法编写为
def any_other_value_present? number, array
!!(array - [ number ]).empty?
end
答案 3 :(得分:1)
以下是一些基准:
require 'fruity'
array = [1, 1, 3, 4]
compare do
chunk_it { array.chunk {|v| v == 1 }.to_a.length == 2 }
include_and_any { array.include?(1) && array.any? {|v| v != 1} }
set_diff { !!(array - [ 1 ]).empty? }
array_count { array.count(1) == array.size }
partition_them { has_one,and_others = array.partition{|n| n == 1}.map(&:any?); has_one && and_others }
end
# >> Running each test 16384 times. Test will take about 5 seconds.
# >> array_count is faster than include_and_any by 4x ± 0.1 (results differ: false vs true)
# >> include_and_any is faster than set_diff by 19.999999999999996% ± 10.0% (results differ: true vs false)
# >> set_diff is faster than partition_them by 2x ± 0.1 (results differ: false vs true)
# >> partition_them is faster than chunk_it by 5x ± 1.0
请注意,其中一些返回的结果与其他结果不同。
答案 4 :(得分:0)
您可以使用相当简单的迭代器来完成此任务。例如,
array = [1,1,3,4]
puts array.any? { |val| 1 != val }
=> true
如果数组中有1
以外的任何内容,则返回true
答案 5 :(得分:0)
如果你需要2个布尔值,我会这样做
a = [1,2,3,4,5]
has_one,and_others = a.partition{|n| n == 1}.map(&:any?)
has_one
#=> true
and_others
#=> true
a = [1,1,1,1,1]
has_one,and_others = a.partition{|n| n == 1}.map(&:any?)
has_one
#=> true
and_others
#=> false
#partition
将数组分成2个数组,首先是块为真,第二个是块为假。
答案 6 :(得分:0)
如何:
class Array
def include_any?(*args)
args.each {|value| return true if include?(value) }
return false
end
def includes?(*args)
args.each {|value| return false unless include?(value)}
return true
end
end
myarray = [:dog, :cat, :cow, :sheep]
#This Will Check if includes all the values given
myarray.includes?(:dog, :cat)
=> true
myarray.includes?(:dog, :horse)
=> false
# This will check if include any of the values given
myarray.includes_any?(:dog, :cat, :horse)
=> true
myarray.includes_any?(:horse, :ox)
=> false
答案 7 :(得分:0)
我使用不同的数组和以下四种方法重新使用@ theTinMan的基准测试:index
,uniq
,delete
和dup_delete
。注意delete
会改变数组。
从一次到另一次运行的结果差异很大,但include_and_any
通常仍能以一个舒适的差距获胜。另请注意,fruity
(我以前没有使用过)会报告完成某些运行需要几分钟,但实际上从不需要超过15秒。
require 'fruity'
def run_em(array, val)
compare do
chunk_it { array.chunk {|v| v == val }.to_a.length == 2 }
include_and_any { array.include?(val) && array.any? {|v| v != 1} }
set_diff { !!(array - [ val ]).empty? }
array_count { array.count(val) == array.size }
partition_them { has_one,and_others = array.partition{|n| n == 1}.map(&:any?)
has_one && and_others }
index { !!(array.index(val) && array.index { |e| e != val }) }
uniq { a = array.uniq; a.include?(val) && (a.size > 1) }
delete { !!array.delete(val) && array.any? }
dup_delete { a = array.dup; !!a.delete(val) && a.any? }
end
end
测试阵列
n = 1_000
only_dups = Array.new(n,0)
all_dups_but_one = only_dups.dup
all_dups_but_one[n/2] = 1
仅重复
仅重复,首次运行
run_em(only_dups, 0)
Running each test 65536 times. Test will take about 9 minutes.
include_and_any is faster than delete by 2x ± 1.0
delete is similar to index
index is faster than uniq by 2x ± 0.1
uniq is similar to array_count (results differ: false vs true)
array_count is faster than dup_delete by 3x ± 1.0 (results differ: true vs false)
dup_delete is faster than set_diff by 2x ± 0.1 (results differ: false vs true)
set_diff is similar to partition_them (results differ: true vs false)
partition_them is faster than chunk_it by 7x ± 1.0
仅重复,第二次运行
run_em(only_dups, 0)
Running each test 65536 times. Test will take about 13 seconds.
include_and_any is similar to delete
delete is similar to index
index is faster than uniq by 2x ± 1.0
uniq is similar to array_count (results differ: false vs true)
array_count is faster than dup_delete by 3x ± 1.0 (results differ: true vs false)
dup_delete is faster than set_diff by 2x ± 0.1 (results differ: false vs true)
set_diff is similar to partition_them (results differ: true vs false)
partition_them is faster than chunk_it by 7x ± 1.0
所有重复但只有一个
所有重复但只有一个,首次运行
run_em(all_dups_but_one, 0)
Running each test 32768 times. Test will take about 4 minutes.
include_and_any is faster than index by 2x ± 1.0
index is similar to delete
delete is similar to uniq
uniq is faster than array_count by 2x ± 0.1
array_count is faster than dup_delete by 2x ± 0.1
dup_delete is faster than set_diff by 2x ± 0.1
set_diff is faster than partition_them by 2x ± 0.1
partition_them is faster than chunk_it by 6x ± 1.0
所有重复但只有一次,第二次运行
run_em(all_dups_but_one, 0)
Running each test 65536 times. Test will take about 12 seconds.
include_and_any is faster than index by 2x ± 1.0
index is similar to delete
delete is similar to uniq
uniq is faster than array_count by 2x ± 1.0
array_count is faster than dup_delete by 2x ± 1.0
dup_delete is faster than set_diff by 2x ± 0.1
set_diff is similar to partition_them
partition_them is faster than chunk_it by 6x ± 1.0