ruby - identify zero and non-zero scenario - with one value (zero) having special meaning -


i comparing values in multiple arrays. want flag values have 'zero' , 'non-zero' @ same time in same array. reason stuck. here test cases / scenarios:

 1. 0 , 0          => ignore   2. non-zero , non-zero  => ignore   3. 0 , non-zero      => after!   4. 0              => ignore   5. non-zero          => ignore 

here code 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 - 0 , other non-zero values unique_with_zero?(1,1,3,1)   #false unique_with_zero?(0)         #false - need multiple values unique_with_zero?(1)         #false    unique_with_zero?(0,0)       #false   

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


Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -