3

I am comparing values in multiple arrays. I want to flag values that have 'zero' and 'non-zero' at the same time in the same array. For some reason I am stuck. Here are test cases / scenarios:

 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

Here is my code so far:

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  
1
  • Can the array contain the (non-zero) value 'cat'?, or are all elements Fixnums or Numeric? Jul 22, 2015 at 0:51

5 Answers 5

2

array.any?(&:zero?) && array.uniq.length > 1

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

works faster than array.uniq, a lot

1
  • 1
    You could write that arr.find(&:zero?) && arr.find(&:nonzero?). Jul 22, 2015 at 4:51
1

Gentlemen, start your engines!

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

I changed excupery's (destructive) arr.uniq! to (the non-destructive) arru = arr.dup, to make it comparable with the other methods, which are all non-destructive.

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

I also ran the benchmark for n = 10_000 and obtained very similar results.

0

Since you're already removing duplicates all you need to check for is if it contains zero and anything else. So after the uniq! call you only need:

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

0

Assuming all array values are Numeric), we can do this with a single pass through the array using Enumerable#minmax_by:

def zero_and_nonzero?(*arr)
  mn, mx = arr.minmax_by(&:abs)
  mn.zero? && !mx.zero?
end

zero_and_nonzero?()          #=> false
zero_and_nonzero?(30,20,40)  #=> false
zero_and_nonzero?(111,0,500) #=> true
zero_and_nonzero?(1,1,3,1)   #=> false
zero_and_nonzero?(0)         #=> false
zero_and_nonzero?(1)         #=> false
zero_and_nonzero?(0,-1)      #=> true
zero_and_nonzero?(0.0,1.1)   #=> true

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.