识别零和非零情景 - 一个值(零)具有特殊含义

时间:2015-07-21 22:09:22

标签: ruby

我正在比较多个数组中的值。 我想标记具有“零”值的值。并且'非零'同时在同一个数组中。出于某种原因,我被困住了。 以下是测试用例/场景:

 1. zero and zero          => ignore 
 2. non-zero and non-zero  => ignore 
 3. zero and non-zero      => this is what we are after! 
 4. only zero              => ignore 
 5. only non-zero          => ignore

到目前为止,这是我的代码:

def unique_with_zero?(*arr)
   arr.uniq!
   arr.sort!

   if arr.size == 1
      print 'ignore: ', arr, "\n"
   end

    if arr.size > 2
      print 'candidate: ', arr, "\n"
    end
end

#test cases
unique_with_zero?(30,20,40)  #false
unique_with_zero?(111,0,500) #true - zero and other non-zero values
unique_with_zero?(1,1,3,1)   #false
unique_with_zero?(0)         #false - we need multiple values
unique_with_zero?(1)         #false   
unique_with_zero?(0,0)       #false  

4 个答案:

答案 0 :(得分:2)

Blaze.renderWithData

答案 1 :(得分:1)

arr.find { |elem| elem == 0 } && arr.find { |elem| elem != 0 }

比array.uniq工作得更快,很多

答案 2 :(得分:1)

先生们,启动引擎!

require 'fruity'

def benchem(arr, msg)
  puts msg
  compare do 
    exupery { arru=arr.uniq; arru.size >= 2 && arru.include?(0) }
    krill   { arr.any?(&:zero?) && arr.uniq.length > 1 }
    wired9  { !!(arr.find { |elem| elem == 0 } && arr.find { |elem| elem != 0 }) }
    cary    { mn, mx = arr.minmax_by(&:abs); mn.zero? && !mx.zero? } 
  end
end

我将excupery' s(破坏性)arr.uniq!更改为(非破坏性)arru = arr.dup,以使其与其他方法相比较破坏性的。

def bench_for_n(n)
  arr = Array.new(n) { 0 }
  benchem arr, "\n\nn = #{n}, all 0"

  arr[rand n] = 1
  benchem arr, "\nn = #{n}, one 1, rest 0"

  arr[rand n] = 1
  arr[rand n] = 1
  arr[rand n] = 1
  benchem arr, "\nn = #{n}, four 1's, rest 0"

  arr = Array.new(n) { 1 }
  benchem arr "\nn = #{n}, all 1"

  arr[rand n] = 0
  benchem arr "\nn = #{n}, one 0, rest 1"

  arr[rand n] = 0
  arr[rand n] = 0
  arr[rand n] = 0
  benchem arr, "\nn = #{n}, four 0's, rest 1"
end

bench_for_n(1_000)

n = 1000, all 0
Running each test 128 times. Test will take about 2 seconds.
exupery is similar to krill
krill is faster than wired9 by 4x ± 1.0
wired9 is faster than cary by 2x ± 0.1

n = 1000, one 1, rest 0
Running each test 128 times. Test will take about 1 second.
krill is similar to exupery
exupery is faster than wired9 by 4x ± 1.0
wired9 is faster than cary by 2x ± 0.1

n = 1000, four 1's, rest 0
Running each test 1024 times. Test will take about 5 seconds.
wired9 is faster than krill by 5x ± 1.0
krill is similar to exupery
exupery is faster than cary by 9x ± 1.0

n = 1000, all 1
Running each test 128 times. Test will take about 1 second.
exupery is faster than krill by 3x ± 1.0
krill is faster than wired9 by 2x ± 0.1
wired9 is faster than cary by 2x ± 0.1

n = 1000, one 0, rest 1
Running each test 128 times. Test will take about 1 second.
exupery is faster than krill by 3x ± 1.0
krill is similar to wired9
wired9 is faster than cary by 2x ± 1.0

n = 1000, four 0's, rest 1
Running each test 128 times. Test will take about 1 second.
exupery is faster than krill by 3x ± 0.1
krill is similar to wired9
wired9 is faster than cary by 3x ± 1.0

我还运行了n = 10_000的基准并获得了非常相似的结果。

答案 3 :(得分:0)

由于您已经删除重复项,因此需要检查的是它是否包含零和其他任何内容。所以在uniq!电话之后你只需要:

return arr.size >= 2 && arr.include?(0)