检查数组中两个不同数字的总和是否等于一个可变数字? [英] Check if the sum of two different numbers in an array equal a variable number?
问题描述
在Ruby中,我想获取一个数字数组,选择2个不同的数字,将这2个数字加在一起,然后查看那里的天气等于变量x.y和变量x.这是我使用的代码
In Ruby, I would like to take an array of numbers, select 2 different numbers, add those 2 numbers together and see weather there equal to a variable x.y'd a variable x. Here is the code I used
def arrayIsEqual? (numArray, x)
return true if numArray.sample + numArray.sample == x
return false if numArray.empty? || numArray.count == 1
end
例如
numArray = [4,2,7,5]
x = 11
arrayIsEqual (numArray, n)
应该返回true
,因为4 + 7 = n(11)
arrayIsEqual (numArray, n)
should return true
, since 4 + 7 = n(11)
我如何使它工作?
我不希望它是2个随机数,而只是任意2个不同的数字加起来为n
I don't want it to be 2 random numbers, just any 2 different numbers that add up to n
推荐答案
您似乎正在尝试查看数组中是否有 any 两个数字加起来等于指定的值x
.但是,您的代码只是随机选择两个数字,然后检查这些数字是否加起来.
It looks like you're trying to see if there are any two numbers in the array that add up to the specified value x
. However, your code just picks two numbers at random and checks if those numbers add up.
Ruby具有 Array#combination
方法,该方法可以生成所有给定长度的组合:
Ruby has the Array#combination
method, which generates all combinations of a given length:
def contains_pair_for_sum?(arr, n)
!!arr.uniq.combination(2).detect { |a, b| a + b == n }
end
一些注意事项:
-
首先,我们根据Ruby约定对其进行命名:每个单词均为
separated_by_underscores
.最后的?
表示该方法是谓词方法,并返回true或false值.
First, we named it according to Ruby conventions: each word is
separated_by_underscores
. The?
on the end means that the method is a predicate method and returns a true or false value.
在该方法内部,发生了一些事情.让我们一步一步地看那条线.
Inside the method, a few things happen. Let's look at that line, piece by piece.
-
arr
:我们采用传入的数组.
arr
: We take the array that was passed in.
<...>.uniq
:我们仅查看唯一元素(因为OP希望选择两个不同的数字).
<...>.uniq
: We only look at the unique elements (because the OP wants to pick two different numbers).
<...>.combination(2)
:我们要求长度为2的数组的所有组合.如果数组为[4, 5, 6]
,我们将得到[[4, 5], [4, 6], [5, 6]]
.
<...>.combination(2)
: We ask for all combinations from the array of length 2. If the array was [4, 5, 6]
, we'd get [[4, 5], [4, 6], [5, 6]]
.
<...>.detect { |a, b| a + b == n }
:我们寻找第一个组合起来等于n
的组合.如果找到一个,那就是该方法的结果.否则,我们得到nil
.
<...>.detect { |a, b| a + b == n }
: We look for the first combination that adds up to n
. If we found one, that's the result of that method. Otherwise, we get nil
.
!!<...>
:最后,我们获取从detect
获得的结果并将其取反两次.第一个否定产生一个布尔值(如果我们得到的值是nil
,则为true
;如果其他值为false
,则为false
);第二个否定产生一个布尔值,该布尔值与第一个否定的真值相同.这是Ruby惯用语,用于将结果强制为true
或false
.
!!<...>
: Finally, we take the result we got from detect
and negate it twice. The first negation produces a Boolean value (true
if the value we got was nil
, or false
if it's anything else); the second negation produces a Boolean value that's identical to the truth value of the first negation. This is a Ruby idiom to coerce a result into being either true
or false
.
让我们看看它的作用:
array = [4, 5, 9, 7, 8]
contains_pair_for_sum?(array, 11)
# => true (because [4, 7] sums to 11)
contains_pair_for_sum?(array, 17)
# => true (because [9, 8] sums to 17)
contains_pair_for_sum?(array, 100)
# => false (no pair matched)
这篇关于检查数组中两个不同数字的总和是否等于一个可变数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!