2015-02-23 134 views
2

是否可以确定数组是否包含特定值和其他值?数组包含x和其他数字

array = [1, 1, 3, 4] 

array.include? 1 #=> true 

我可以用include?以确定其是否包含数字1,但在那之后,我怎么检查它是否含有比其他1值,不关心什么,这些价值观是?

回答

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, 
+0

您的返回值是倒退的。你需要在某处使用'!'。 – 2015-02-23 21:08:01

+0

我澄清了我的答案 - 谢谢你的提示。 – knut 2015-02-23 21:45:32

1

你可以写一个方法

def any_other_value_present? number, array 
    !!(array - [ number ]).empty? 
end 
+2

'[array - [number])''应该是'(array - [number])' – 2015-02-23 21:06:58

+0

@theTinMan Thanks .. Overlooked – 2015-02-23 21:10:21

0

你可以使用一个相当简单的迭代器来完成这个任务。例如,

array = [1,1,3,4] 
puts array.any? { |val| 1 != val } 
=> true 

因此,这将如果有比1其他任何数组中

+0

这不适用于数组,例如在OP的示例中。 – Ajedi32 2015-02-23 19:55:38

+0

好点。我改变了一种不同的迭代方法,所以它现在可以工作。 – jkeuhlen 2015-02-23 20:02:38

+0

为什么不使用'all?'而不是'!='例如'array.all? {| VAL | val == 1}'因为'all?'是'any?'的倒数 – engineersmnky 2015-02-23 20:14:06

2

可能有多种方式,以更好地回答这个问题返回true,但不理解的是如何更广泛的范围内你正在使用这些信息,很难给出准确的答案。

这就是说,你有很多选择。我不知道有一个单一的操作,你可以做测试,但一个聪明的解决方案可能是这样的:

array.chunk {|v| v == 1 }.to_a.length == 2 

这是什么要做的就是返回块结果和值的数组匹配块,结果。如果该数组的长度为2,那么您知道该数组的值都匹配且不匹配1

尽管这是Θ(n)可以实现具有多片的代码,例如更快的解决方案:

array.include?(1) && array.any? {|v| v != 1} 
+0

我会写它'array.any?{| i | i == 1} && array.any?{| i | i!= 1}#=> true',但根据基准'include?'可以使其速度提高50%。 – 2015-02-23 20:56:35

+0

第二个不仅更快,它以非常容易阅读的方式表达问题。 – 2015-02-23 21:44:42

+0

@theTinMan任何时候你都可以避免块绑定和调用,通常你会看到速度的提升。 – 2015-02-23 21:58:09

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个数组首先是该块是真正的第二个是块是假的。

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 

注意,他们夫妇返回结果被其他人不一样。

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 

0

我重新@ theTinMan的基准用以下所示的不同的阵列和四个以上的方法:indexuniqdeletedup_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 
相关问题