[英]Find if n exists as a sum of any 2 numbers in the given array
I am trying to find whether n
exists as a sum of any two numbers in the passed array if so return true
else false
, the problem with my code is that inject
is not iterating as I want it to. 我正在尝试查找
n
是否作为所传递数组中任何两个数字的和而存在,如果返回true
,则返回false
,我的代码的问题是inject
没有按照我想要的那样进行迭代。 What am I doing wrong? 我究竟做错了什么?
def sum_to_n?(array,n)
array.each do |i|
array.inject(i) do |memo,var|
if memo + var == n
return true
else
return false
end
end
end
end
puts sum_to_n?([1,2,3,4,5],9)
Here is an approach : 这是一种方法:
def sum_to_n?(a,n)
!!a.find{|e| a.include?(n-e)}
end
a = [1,2,3,4,5]
sum_to_n?(a,9) # => true
sum_to_n?(a,11) # => false
If you want to get those 2 elements: 如果要获得这两个元素:
def sum_to_n?(a,n)
num=a.find{|e| a.include?(n-e)}
unless num
puts "not exist"
else
p [n-num,num]
end
end
a = [1,2,3,4,5]
sum_to_n?(a,9)
# >> [5, 4]
sum_to_n?(a,11)
# >> not exist
Logic 逻辑
Enumerable#find
method passing one array element per iteration.Now for any iteration,say I have an element e
,and I subtracted it from n. Enumerable#find
方法每次迭代传递一个数组元素。现在对于任何迭代,假设我都有一个元素e
,然后从n中减去它。 Now I was just testing that (ne)
is present in the source array.If I found a match #find
will stop finding,and immediately will return e
.If not found,then it will go for next iteration. 现在我只是在测试源数组中是否存在
(ne)
如果我发现匹配项#find
将停止查找,并立即返回e
。如果未找到,则将进行下一次迭代。 If #find
completes its iteration,but didn't find (ne)
,as per the documentation it will return nil
. 如果
#find
完成其迭代,但未找到(ne)
,则根据文档,它将返回nil
。
This question have already be answered but I think this approach is more readable: 这个问题已经得到解答,但我认为这种方法更具可读性:
def sum_to_n?(a,n)
a.combination(2).find{|x,y| x+y==n}
end
a = [1,2,3,4,5]
p sum_to_n?(a,9) # => [4, 5]
p sum_to_n?(a,11) # => nil
Never one for doing things the easy way: 永远不要以一种简单的方式做事:
n = 14
a = [1,3,5,9,13,3,18]
if n==0
a.select {|x| x == 0}.size > 1
else
a.map {|x| 2*x - n}.uniq.group_by(&:abs).values.map(&:size).max > 1 # => true
end
a
has duplicates (the two 3's). a
具有重复项(两个3)。 Without uniq, we'd be in trouble at the next step. arr = [1, 2,3,4,5,6,7]
num = 7 (given number for 2 element combination)
Below program will just return single combination 下面的程序只会返回单个组合
arr.uniq.combination(2).detect {|a, b| a + b == num}
=> [2, 5]
where as below program will return all possible combination 如下所示,程序将返回所有可能的组合
arr.uniq.combination(2).find_all {|a, b| a +b == num}
=> [[2, 5], [3, 4]]
RUBY 红宝石
This code will reject the empty arrays and returns the proper array with values. 此代码将拒绝空数组,并返回带有值的正确数组。
def find_sequence(val, num) def find_sequence(val,num)
b = val.length (0..b - 1).map {|n| b = val.length(0..b-1).map {| n | val.uniq.combination(n).each.find_all {|value|
val.uniq.combination(n).each.find_all {| value | value.reduce(:+) == num}}.reject(&:empty?)
value.reduce(:+)== num}}。reject(&:empty?)
end 结束
val = [-10, 1, -1, 2, 0] num = 2 val = [-10,1,-1,2,0] num = 2
Output will be [[2],[2,0],[-1,1,2],[-1,1,2,0]] 输出将为[[2],[2,0],[-1,1,2],[-1,1,2,0]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.