Ruby 检查数组并实现bool方法
你有一个数组。如果数组中任意两个数字相加为零,则返回Ruby 检查数组并实现bool方法,ruby,arrays,boolean,Ruby,Arrays,Boolean,你有一个数组。如果数组中任意两个数字相加为零,则返回true。不管有多少对,只要有一对相加为零,就返回true。如果存在零,则只有在存在多个零的情况下才能返回true 我编写了两个函数,一个用于检查每个函数,最后一个用于组合这两个函数,如果其中一个函数不满足,则返回false def checkZero(array) zerocount = 0 for j in 0..array.count if array[j] == 0 zerocount += 1 en
true
。不管有多少对,只要有一对相加为零,就返回true
。如果存在零,则只有在存在多个零的情况下才能返回true
我编写了两个函数,一个用于检查每个函数,最后一个用于组合这两个函数,如果其中一个函数不满足,则返回false
def checkZero(array)
zerocount = 0
for j in 0..array.count
if array[j] == 0
zerocount += 1
end
end
if zerocount > 1 #this part seems to not be working, not sure why
return true
else
return false
end
end
def checkNegative(array)
for j in 0..array.count
neg = -array[j] #set a negative value of the current value
if array.include?(neg) #check to see whether the negative exists in the array
return true
else
return false
end
end
end
def checkArray(array)
if checkZero(array) == true or checkNegative(array) == true
return true
else
return false
end
end
然后运行类似于
array = [1,2,3,4,0,1,-1]
checkArray(array)
到目前为止,Ruby没有归还任何东西。我只是得到一个空白。我感觉我的
返回值不正确。问题可能是您没有输出结果
array = [1,2,3,4,0,1,-1]
puts checkArray(array)
如果性能(O(n^2))不是一个很大的问题,那么checkArray
方法可以像下面这样编写:
def check_array(array)
array.combination(2).any?{|p| p.reduce(:+) == 0}
end
更有效的(O(n logn))解决方案是:
def check_array(array)
array.sort! # `array = array.sort` if you need the original array unchanged
i, j = 0, array.size - 1
while i < j
sum = array[i] + array[j]
if sum > 0
j -= 1
elsif sum < 0
i += 1
else
return true
end
end
return false
end
def check_数组(数组)
array.sort!#`array=array.sort`如果需要原始数组保持不变
i、 j=0,array.size-1
而我
总和=数组[i]+数组[j]
如果总和>0
j-=1
elsif和<0
i+=1
其他的
返回真值
结束
结束
返回错误
结束
问题可能是您没有输出结果
array = [1,2,3,4,0,1,-1]
puts checkArray(array)
如果性能(O(n^2))不是一个很大的问题,那么checkArray
方法可以像下面这样编写:
def check_array(array)
array.combination(2).any?{|p| p.reduce(:+) == 0}
end
更有效的(O(n logn))解决方案是:
def check_array(array)
array.sort! # `array = array.sort` if you need the original array unchanged
i, j = 0, array.size - 1
while i < j
sum = array[i] + array[j]
if sum > 0
j -= 1
elsif sum < 0
i += 1
else
return true
end
end
return false
end
def check_数组(数组)
array.sort!#`array=array.sort`如果需要原始数组保持不变
i、 j=0,array.size-1
而我
总和=数组[i]+数组[j]
如果总和>0
j-=1
elsif和<0
i+=1
其他的
返回真值
结束
结束
返回错误
结束
我无法重现您的代码中的任何问题,但您可以非常简洁地表达解决方案,使用获取所有可能的对,然后将每对求和,最后检查是否有:
我无法重现您的代码中的任何问题,但您可以非常简洁地表达解决方案,使用获取所有可能的对,然后用求和,最后检查是否有:
这是一个小问题。让我们从第一种方法开始:
def checkZero(array)
Ruby命名约定是snake_大小写而不是camelCase。这应该是def check\u zero(数组)
现在循环:
zerocount = 0
for j in 0..array.count
if array[j] == 0
zerocount += 1
end
end
正如@AndrewMarshall所说,
的不是惯用语<代码>每个
都是首选。然而,在ruby中,由于数组
和可枚举
(包含在数组
中)上的所有可用方法,几乎不需要在循环之前初始化变量。我强烈建议将这些方法提交到内存中。以上可以写出来
array.any? {|number| number.zero?}
或同等地
array.any?(&:zero?)
现在,这部分:
if zerocount > 1 #this part seems to not be working, not sure why
return true
else
return false
end
end
只要你有图案
if (expr that returns true or false)
return true
else
return false
end
它可以简化为简单的返回(返回true或false的expr)
。如果它是方法的最后一条语句,甚至可以省略return
将所有内容放在一起:
def check_zero(array)
array.any?(&:zero?)
end
def check_zero_sum(array)
array.combination(2).any?{|a,b| a + b == 0}
end
def check_array(array)
check_zero(array) || check_zero_sum(array)
end
(注意,我借用了AndrewMarshall关于检查零和的代码,我认为这很容易理解,但是@CarySwoveland的答案会更快)
编辑
我忽略了一个事实,check\u zero
甚至没有必要,因为您至少需要一对,在这种情况下,check\u zero\u sum
就是您所需要的
def check_array(array)
array.combination(2).any?{|a,b| a + b == 0}
end
这是一个小问题。让我们从第一种方法开始:
def checkZero(array)
Ruby命名约定是snake_大小写而不是camelCase。这应该是def check\u zero(数组)
现在循环:
zerocount = 0
for j in 0..array.count
if array[j] == 0
zerocount += 1
end
end
正如@AndrewMarshall所说,的不是惯用语<代码>每个
都是首选。然而,在ruby中,由于数组
和可枚举
(包含在数组
中)上的所有可用方法,几乎不需要在循环之前初始化变量。我强烈建议将这些方法提交到内存中。以上可以写出来
array.any? {|number| number.zero?}
或同等地
array.any?(&:zero?)
现在,这部分:
if zerocount > 1 #this part seems to not be working, not sure why
return true
else
return false
end
end
只要你有图案
if (expr that returns true or false)
return true
else
return false
end
它可以简化为简单的返回(返回true或false的expr)
。如果它是方法的最后一条语句,甚至可以省略return
将所有内容放在一起:
def check_zero(array)
array.any?(&:zero?)
end
def check_zero_sum(array)
array.combination(2).any?{|a,b| a + b == 0}
end
def check_array(array)
check_zero(array) || check_zero_sum(array)
end
(注意,我借用了AndrewMarshall关于检查零和的代码,我认为这很容易理解,但是@CarySwoveland的答案会更快)
编辑
我忽略了一个事实,check\u zero
甚至没有必要,因为您至少需要一对,在这种情况下,check\u zero\u sum
就是您所需要的
def check_array(array)
array.combination(2).any?{|a,b| a + b == 0}
end
以下是一些相对有效的方法来检查任意两个值的和是否为零: 解决方案#1
def checkit(a)
return true if a.count(&:zero?) > 1
b = a.uniq.map(&:abs)
b.uniq.size < b.size
end
解决方案#3
def checkit(a)
return true if a.count(&:zero?) > 1
pos, non_pos = a.group_by { |n| n > 0 }.values
(pos & non_pos.map { |n| -n }).any?
end
return true if a.count(&:zero?) > 1
#=> return true if 1 > 1
h = a.group_by { |n| n > 0 }
#=> {true=>[1, 3, 4, 2, 2], false=>[-3, -5, -7, 0]}
b = h.values
#=> [[1, 3, 4, 2, 2], [-3, -5, -7, 0]]
pos, non_pos = b
pos
#=> [1, 3, 4, 2, 2]
non_pos
#=> [-3, -5, -7, 0]
c = non_pos.map { |n| -n }
#=> [3, 5, 7, 0]
d = pos & c
#=> [3]
d.any?
#=> true
解决方案#4
require 'set'
def checkit(a)
a.each_with_object(Set.new) do |n,s|
return true if s.include?(-n)
s << n
end
false
end
require 'set'
enum = a.each_with_object(Set.new)
#=> #<Enumerator: [1, 3, 4, 2, 2, -3, -5, -7, 0]:each_with_object(#<Set: {}>)>
enum.to_a
#=> [[1, #<Set: {}>],
# [3, #<Set: {}>],
# ...
# [0, #<Set: {}>]]
解释
checkit([1, 3, 4, 2, 2,-3,-5,-7, 0, 0]) #=> true
checkit([1, 3, 4, 2, 2,-3,-5,-7, 0]) #=> true
checkit([1, 3, 4, 2,-3, 2,-3,-5,-7, 0]) #=> true
checkit([1, 3, 4, 2, 2,-5,-7, 0]) #=> false
以下内容均指阵列:
a = [1,3,4,2,2,-3,-5,-7,0]
#1
def checkit(a)
return true if a.count(&:zero?) > 1
b = a.uniq.map(&:abs)
b.uniq.size < b.size
end
零表示有点问题,所以让我们先看看是否有多个,在这种情况下,我们就完成了。由于a.count(&:zero?)#=>1
,a.count(&:zero?)>1#=>false
,所以
return true if a.count(&:zero?) > 1
不会让我们回来。接下来,我们删除所有重复项:
a.uniq #=> [1, 3, 4, 2, -3, -5, -7, 0]
然后将所有数字转换为其绝对值:
b = a.uniq,map(&:abs) #=> [1, 3, 4, 2, 3, 5, 7, 0]
最后查看c
是否包含任何DUP,这意味着原始数组至少包含两个符号相反的非零数字:
c.uniq.size < c.size #=> true
因此,返回的是true
#3
def checkit(a)
return true if a.count(&:zero?) > 1
pos, non_pos = a.group_by { |n| n > 0 }.values
(pos & non_pos.map { |n| -n }).any?
end
return true if a.count(&:zero?) > 1
#=> return true if 1 > 1
h = a.group_by { |n| n > 0 }
#=> {true=>[1, 3, 4, 2, 2], false=>[-3, -5, -7, 0]}
b = h.values
#=> [[1, 3, 4, 2, 2], [-3, -5, -7, 0]]
pos, non_pos = b
pos
#=> [1, 3, 4, 2, 2]
non_pos
#=> [-3, -5, -7, 0]
c = non_pos.map { |n| -n }
#=> [3, 5, 7, 0]
d = pos & c
#=> [3]
d.any?
#=> true
#4
require 'set'
def checkit(a)
a.each_with_object(Set.new) do |n,s|
return true if s.include?(-n)
s << n
end
false
end
require 'set'
enum = a.each_with_object(Set.new)
#=> #<Enumerator: [1, 3, 4, 2, 2, -3, -5, -7, 0]:each_with_object(#<Set: {}>)>
enum.to_a
#=> [[1, #<Set: {}>],
# [3, #<Set: {}>],
# ...
# [0, #<Set: {}>]]
require'set'
enum=a.每个带有_对象的_(Set.new)
#=> #
列举
#=> [[1, #],
# [3, #],
# ...
# [0, #]]
将值传递到块中,分配给块变量,然后执行块,如下所示:
n, s = enum.next
#=> [1, #<Set: {}>]
s.include?(-n)
#=> #<Set: {}>.include?(-1)
#=> false
s << n
#=> #<Set: {1}>
n, s = enum.next
#=> [3, #<Set: {1}>]
s.include?(-3)
#=> false
s << n
#=> #<Set: {1, 3}>
...
n, s = enum.next
#=> [2, #<Set: {1, 3, 4, 2}>]
s.include?(-n)
#=> false
s << n
#=> #<Set: {1, 3, 4, 2}> # no change
n, s = enum.next
#=> [-3, #<Set: {1, 3, 4, 2}>]
s.include?(-n)
#=> true
n,s=enum.next
#=> [1, #]
s、 包括?(-n)
#=>#。包括?(-1)
#=>错误
#
n、 s=enum.next
#=> [3, #]
s、 包括?(-3)
#=>错误
s#
...
n、 s=enum.next
#=> [2, #]
s、 包括?(-n)
#=>错误
没有变化
N